uniku
Reference

ULID

Sortable, URL-safe identifiers using Crockford Base32.

ULIDs sort by creation time and stay shorter than a hyphenated UUID. Use them when the value will often appear in URLs, logs, or APIs.

import { ulid } from 'uniku/ulid'

const eventId = ulid()
// '01KXBK40T3EQW5BGTMQRX89PTA' (string)

Generated API reference

Public methods

Read source

Generate a ULID string or write its 16 canonical bytes into a buffer. ULIDs are URL-safe, carry a millisecond timestamp, and sort by creation time.

ulid

Generate a time-ordered ULID string.

ulid(): string

Generate a ULID with explicit options or write its 16 canonical bytes into a caller-owned buffer.

ulid<TBuf extends Uint8Array = Uint8Array>(options: UlidOptions | undefined, buf: TBuf, offset?: number): TBuf

Generate a ULID string with optional timestamp or random bytes.

ulid(options?: UlidOptions, buf?: undefined, offset?: number): string

UlidOptions

random?

16 bytes of random data to use for ULID generation. Only the first 10 bytes are used.

Uint8Array
msecs?

Timestamp in milliseconds since Unix epoch. Defaults to Date.now().

number
Exampleulid()// => '01KXBK40T3EQW5BGTMQRX89PTA'
toBytes

Convert a ULID string to its canonical 16-byte representation.

toBytes(id: string): Uint8Array
Exampleulid.toBytes('01KXBK40T3EQW5BGTMQRX89PTA')// => Uint8Array [1, 159, 87, 50, 3, 67, 117, 248, 85, 195, 84, 190, 58, 132, 219, 74]
fromBytes

Convert 16 canonical ULID bytes to a ULID string.

fromBytes(bytes: Uint8Array): string
Exampleulid.fromBytes(Uint8Array.from([1, 159, 87, 50, 3, 67, 117, 248, 85, 195, 84, 190, 58, 132, 219, 74]))// => '01KXBK40T3EQW5BGTMQRX89PTA'
timestamp

Read the embedded Unix timestamp in milliseconds.

timestamp(id: string): number
Exampleulid.timestamp('01KXBK40T3EQW5BGTMQRX89PTA')// => 1783874323267
isValid

Return whether a value is a syntactically valid ULID string.

isValid(id: unknown): id is string
Exampleulid.isValid('01KXBK40T3EQW5BGTMQRX89PTA')// => true
NIL

The nil ULID (all zeros)

NIL: string
Exampleulid.NIL// => '00000000000000000000000000'
MAX

The max ULID (maximum valid value)

MAX: string
Exampleulid.MAX// => '7ZZZZZZZZZZZZZZZZZZZZZZZZZ'