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
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(): stringGenerate 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): TBufGenerate a ULID string with optional timestamp or random bytes.
ulid(options?: UlidOptions, buf?: undefined, offset?: number): stringExampleulid()// => '01KXBK40T3EQW5BGTMQRX89PTA'- toBytes
Convert a ULID string to its canonical 16-byte representation.
toBytes(id: string): Uint8ArrayExampleulid.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): stringExampleulid.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): numberExampleulid.timestamp('01KXBK40T3EQW5BGTMQRX89PTA')// => 1783874323267- isValid
Return whether a value is a syntactically valid ULID string.
isValid(id: unknown): id is stringExampleulid.isValid('01KXBK40T3EQW5BGTMQRX89PTA')// => true- NIL
The nil ULID (all zeros)
NIL: stringExampleulid.NIL// => '00000000000000000000000000'- MAX
The max ULID (maximum valid value)
MAX: stringExampleulid.MAX// => '7ZZZZZZZZZZZZZZZZZZZZZZZZZ'