src/nulid

Source   Edit  

Note: There are 2 defines that can be passed to the compiler to trigger different functionality in this library at runtime, they are listed here:

  • --define:nulidInsecureRandom: Uses std/random instead of std/sysrand.
  • --define:nulidNoLocks: Disables the use of locks.

The JS backend used -d:nulidNoLocks by default.

Types

ULID = object
  timestamp*: int64
  when not defined(js):
      randomness*: UInt128

  else:
      randomness*: JsBigInt

  
An object representing a ULID. Source   Edit  
ULIDDefect = object of Defect
Source   Edit  
ULIDError = object of CatchableError
Source   Edit  
ULIDGenerator = ref object
  when NoLocks:
      when not defined(js):
        
      else:
        
      when InsecureRandom:
        
    
  else:
      lock*: RLock
      when InsecureRandom:
        
    
  
A ULID generator object, contains details needed to follow the spec. A generator was made to be compliant with the ULID spec and also to be threadsafe not use globals that could change. Source   Edit  

Procs

func `$`(ulid: ULID): string {....raises: [], tags: [], forbids: [].}
Returns the string representation of a ULID.

Example:

echo $ulid()
Source   Edit  
proc `%`(u: ULID): JsonNode {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc `==`(a, b: ULID): bool {....raises: [], tags: [], forbids: [].}
Source   Edit  
func fromBytes(__520094038: typedesc[ULID]; ulidBytes: openArray[byte]): ULID

Parses a byte array to a ULID..

Note: This isn't available for the JS backend.

Source   Edit  
func fromInt128(__520094026: typedesc[ULID]; val: Int128): ULID

Parses an Int128 to a ULID.

Note: On the JS backend this accepts a JsBigInt from std/jsbigints

Source   Edit  
proc initUlidGenerator(): ULIDGenerator {....raises: [], tags: [], forbids: [].}
Initialises a ULIDGenerator for use. Source   Edit  
func parse(__520094043: typedesc[ULID]; ulidStr: string): ULID
Parses a ULID from a string.

Example:

echo ULID.parse("01H999MBGTEA8BDS0M5AWEBB1A")
Source   Edit  
proc to(j: JsonNode; __520094092: typedesc[ULID]): ULID
Source   Edit  
func toBytes(ulid: ULID): array[16, byte] {....raises: [], tags: [], forbids: [].}

Allows for a ULID to be converted to a byte array for the binary format.

Note: This isn't available for the JS backend.

Example:

let
  ulid = ULID.parse("01H999MBGTEA8BDS0M5AWEBB1A")
  ulidBytes = [1.byte, 138, 82, 154, 46, 26, 114, 144, 182, 228, 20, 42, 184, 229, 172, 42]

assert ulid == ULID.fromBytes(ulidBytes)
Source   Edit  
func toInt128(ulid: ULID): Int128 {....raises: [], tags: [], forbids: [].}

Allows for a ULID to be converted to an Int128.

Note: On the JS backend this returns a JsBigInt from std/jsbigints

Example:

echo ulid().toInt128()
Source   Edit  
proc ulid(gen: ULIDGenerator; timestamp = LowInt48; randomness = LowUint80): ULID {.
    ...gcsafe, raises: [ULIDGenerationError], tags: [TimeEffect], forbids: [].}

Generate a ULID, if timestamp is equal to 0, the randomness parameter will be ignored.

See also:

Example:

let gen = initUlidGenerator()

echo gen.ulid()
Source   Edit  
proc ulid(timestamp = LowInt48; randomness = LowUint80): ULID {.
    ...raises: [ULIDGenerationError], tags: [TimeEffect], forbids: [].}

Generate a ULID using the global generator.

See also:

Example:

echo ulid()
Source   Edit