TypeID
UUID v7 identifiers with a readable, domain-specific prefix.
TypeID wraps a UUID v7 suffix in a lowercase prefix such as user_ or order_. It is useful when a public identifier should reveal the kind of resource it identifies.
import { typeid } from 'uniku/typeid'
const userId = typeid('user')
// 'user_01kxbk40t3egn9xec2thzpa2w7' (string)
typeid.prefix(userId) // 'user'Generated API reference
Public methods
Generate a TypeID string backed by UUID v7. TypeID combines a lowercase snake_case type prefix with a 26-character modified base32 encoding of a UUID v7, producing sortable domain identifiers such as `user_01h2xcejqtf2nbrexx3vqjhp41`.
- typeid
Generate a TypeID from a lowercase entity prefix and a UUID v7 suffix.
typeid(prefix: string, options?: TypeidOptions): stringExampletypeid('user')// => 'user_01kxbk40t3egn9xec2thzpa2w7'- toBytes
Convert a TypeID's UUID v7 suffix to its canonical 16-byte representation.
toBytes(id: string): Uint8ArrayExampletypeid.toBytes('user_01kxbk40t3egn9xec2thzpa2w7')// => Uint8Array [1, 159, 87, 50, 3, 67, 116, 42, 158, 185, 130, 212, 127, 101, 11, 135]- fromBytes
Build a TypeID from a prefix and canonical UUID v7 bytes.
fromBytes(prefix: string, bytes: Uint8Array): stringExampletypeid.fromBytes('user', Uint8Array.from([1, 159, 87, 50, 3, 67, 116, 42, 158, 185, 130, 212, 127, 101, 11, 135]))// => 'user_01kxbk40t3egn9xec2thzpa2w7'- toUuid
Convert a TypeID to its UUID v7 string.
toUuid(id: string): stringExampletypeid.toUuid('user_01kxbk40t3egn9xec2thzpa2w7')// => '019f5732-0343-742a-9eb9-82d47f650b87'- fromUuid
Build a TypeID from a prefix and UUID v7 string.
fromUuid(prefix: string, uuid: string): stringExampletypeid.fromUuid('user', '019f5732-0343-742a-9eb9-82d47f650b87')// => 'user_01kxbk40t3egn9xec2thzpa2w7'- timestamp
Read the UUID v7 timestamp embedded in a TypeID, in milliseconds.
timestamp(id: string): numberExampletypeid.timestamp('user_01kxbk40t3egn9xec2thzpa2w7')// => 1783874323267- prefix
Read a TypeID's entity prefix.
prefix(id: string): stringExampletypeid.prefix('user_01kxbk40t3egn9xec2thzpa2w7')// => 'user'- suffix
Read a TypeID's UUID v7 suffix.
suffix(id: string): stringExampletypeid.suffix('user_01kxbk40t3egn9xec2thzpa2w7')// => '01kxbk40t3egn9xec2thzpa2w7'- isValid
Return whether a value is a syntactically valid TypeID.
isValid(id: unknown): id is stringExampletypeid.isValid('user_01kxbk40t3egn9xec2thzpa2w7')// => true