Skip to content

Function: id()

id(length?): string

Mint a v4 UUID, or a short hex slice of one when length is set.

  • id() returns a full RFC 4122 v4 UUID. Pick this when global uniqueness matters: long-running batches, ids that cross a storage / process boundary, anything that may collide across machines.
  • id(length) returns the first length hex chars of a fresh UUID with dashes stripped (e.g. id(8) -> "a3f1c92b"). Pick this when the id has to be short / typeable and the scope is bounded - cache keys local to a request, slug suffixes. length <= 0 throws.

Prefers crypto.randomUUID(), which covers Node (>= 19) and a browser on a secure origin. Browsers gate randomUUID behind a secure context, so a page served over plain http (a LAN dev host) has crypto but not that method; this package is browser-safe and its callers mint ids on the render path, so it degrades instead of throwing: getRandomValues when present, else Math.random. Every branch returns a well-formed v4 UUID - only the entropy source differs.

number

string

id(); // "123e4567-e89b-12d3-a456-426614174000"
id(8); // "a3f1c92b"