generateId function is a utility for generating unique identifiers (UUIDs) in different versions and output formats. This function supports both UUID v4 (random) and UUID v7 (time-ordered) formats, and can return the result as either a text string or binary data.ts
import { generateId } from "@arkyn/shared";
type (required)"text" returns the UUID as a standard hyphenated string (e.g., "550e8400-e29b-41d4-a716-446655440000")"binary" returns the UUID as a Uint8Array containing the raw binary representation, which is more compact and efficient for storage or transmission"text" | "binary"format (required)"v4" generates a random UUID (version 4) where all bits except the version and variant bits are randomly generated, providing excellent uniqueness with minimal collision probability"v7" generates a time-ordered UUID (version 7) that includes a timestamp component, making it sortable by creation time and useful for databases and time-series data"v4" | "v7"type is "text", returns a string in the standard UUID format with hyphens (e.g., "550e8400-e29b-41d4-a716-446655440000")type is "binary", returns a Uint8Array of 16 bytes representing the UUID in binary formatstring | Uint8Arraytypescript
import { generateId } from "@arkyn/shared";const id = generateId("text", "v4");console.log(id);// Output: "550e8400-e29b-41d4-a716-446655440000" (example)
type or format parameters is provided. This should not occur in TypeScript due to type checking, but can happen if invalid values are passed at runtime.typescript
import { generateId } from "@arkyn/shared";try {const id = generateId("invalid_type" as any, "v4");} catch (error) {console.error(error);// Output: Error: Invalid type or format}
uuid library internally to generate standards-compliant UUIDs according to RFC 4122 (v4) and the draft specification for UUID v7.