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
ULIDGenerationDefect = object of ULIDDefect
- Source Edit
ULIDGenerationError = object of ULIDError
- 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 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 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 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