RFC9562 UUIDs
—
Predefined UUID constants and conversion functions for working with different UUID formats and versions.
Predefined UUID constants for special values and common use cases.
/**
* The nil UUID string (all zeros)
* Represents a UUID that explicitly indicates "no value" or "null UUID"
*/
const NIL: string; // '00000000-0000-0000-0000-000000000000'
/**
* The max UUID string (all ones)
* Represents the maximum possible UUID value
*/
const MAX: string; // 'ffffffff-ffff-ffff-ffff-ffffffffffff'Usage Examples:
import { NIL, MAX, validate } from "uuid";
// Use NIL UUID as default/empty value
let currentSessionId: string = NIL;
function startSession() {
currentSessionId = v4(); // Generate new session ID
}
function endSession() {
currentSessionId = NIL; // Reset to nil value
}
function hasActiveSession(): boolean {
return currentSessionId !== NIL;
}
// Use MAX UUID for range operations
function isUuidInRange(uuid: string, min: string = NIL, max: string = MAX): boolean {
return validate(uuid) && uuid >= min && uuid <= max;
}
// Constants are valid UUIDs
console.log(validate(NIL)); // ⇨ true
console.log(validate(MAX)); // ⇨ truePredefined namespace UUIDs for v3 and v5 generation, available both as properties on the respective functions and as direct exports.
/**
* DNS namespace UUID for v3 generation
* Standard namespace for domain names
*/
v3.DNS: string; // '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
/**
* URL namespace UUID for v3 generation
* Standard namespace for URLs
*/
v3.URL: string; // '6ba7b811-9dad-11d1-80b4-00c04fd430c8'
/**
* DNS namespace UUID for v5 generation (same as v3.DNS)
* Standard namespace for domain names
*/
v5.DNS: string; // '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
/**
* URL namespace UUID for v5 generation (same as v3.URL)
* Standard namespace for URLs
*/
v5.URL: string; // '6ba7b811-9dad-11d1-80b4-00c04fd430c8'
/**
* DNS namespace UUID (direct export)
* Same value as v3.DNS and v5.DNS
*/
const DNS: string; // '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
/**
* URL namespace UUID (direct export)
* Same value as v3.URL and v5.URL
*/
const URL: string; // '6ba7b811-9dad-11d1-80b4-00c04fd430c8'Usage Examples:
import { v3, v5, DNS, URL } from "uuid";
// DNS-based UUIDs for domain names
const domainId = v5('example.com', v5.DNS);
const subdomainId = v5('api.example.com', v5.DNS);
// URL-based UUIDs for web resources
const pageId = v5('https://example.com/page', v5.URL);
const apiId = v5('https://api.example.com/users', v5.URL);
// Use direct exports (equivalent to function properties)
const directDnsId = v5('example.com', DNS);
const directUrlId = v5('https://example.com/page', URL);
// All equivalent ways to access namespace constants
console.log(v3.DNS === v5.DNS); // ⇨ true
console.log(v3.URL === v5.URL); // ⇨ true
console.log(DNS === v3.DNS); // ⇨ true
console.log(URL === v3.URL); // ⇨ true
// Generate consistent IDs across runs
function getResourceId(url: string): string {
return v5(url, v5.URL); // Always returns same UUID for same URL
}Functions to convert between UUID version 1 and version 6, which have compatible timestamp information but different field ordering.
/**
* Convert version 1 UUID to version 6 UUID
* Reorders timestamp fields for better database performance and sorting
* @param uuid - Version 1 UUID (string or bytes)
* @returns Version 6 UUID in same format as input
*/
function v1ToV6(uuid: string): string;
function v1ToV6(uuid: Uint8Array): Uint8Array;
/**
* Convert version 6 UUID to version 1 UUID
* Restores original v1 timestamp field ordering
* @param uuid - Version 6 UUID (string or bytes)
* @returns Version 1 UUID in same format as input
*/
function v6ToV1(uuid: string): string;
function v6ToV1(uuid: Uint8Array): Uint8Array;Usage Examples:
import { v1, v6, v1ToV6, v6ToV1, version } from "uuid";
// Generate v1 UUID and convert to v6
const v1Id = v1();
const v6Id = v1ToV6(v1Id);
console.log(version(v1Id)); // ⇨ 1
console.log(version(v6Id)); // ⇨ 6
// Round-trip conversion preserves timestamp information
const backToV1 = v6ToV1(v6Id);
console.log(v1Id === backToV1); // ⇨ true
// Convert binary UUIDs
const v1Bytes = new Uint8Array(16);
v1(undefined, v1Bytes);
const v6Bytes = v1ToV6(v1Bytes);
const backToV1Bytes = v6ToV1(v6Bytes);
console.log(v1Bytes.every((byte, i) => byte === backToV1Bytes[i])); // ⇨ true
// Database optimization: convert existing v1 UUIDs to v6 for better indexing
function migrateUuidsToV6(v1Uuids: string[]): string[] {
return v1Uuids.map(uuid => {
if (version(uuid) === 1) {
return v1ToV6(uuid);
}
return uuid; // Leave non-v1 UUIDs unchanged
});
}
// Performance comparison
const v1Uuid = v1();
const v6Uuid = v1ToV6(v1Uuid);
// v6 UUIDs sort chronologically better than v1
const timestampedIds = [
v1(), v1(), v1(), v1(), v1()
].map(id => v1ToV6(id));
timestampedIds.sort(); // Natural chronological orderingCommon patterns using constants and conversions together:
import { NIL, MAX, v1, v6, v1ToV6, v6ToV1, v5, validate } from "uuid";
// Initialize with NIL, upgrade to timestamped UUIDs
class SessionManager {
private sessionId: string = NIL;
start(): string {
this.sessionId = v6(); // Use v6 for better DB performance
return this.sessionId;
}
end(): void {
this.sessionId = NIL;
}
isActive(): boolean {
return this.sessionId !== NIL;
}
getId(): string {
return this.sessionId;
}
}
// UUID range operations using constants
function createUuidRange(start: string = NIL, end: string = MAX) {
if (!validate(start) || !validate(end)) {
throw new Error('Invalid UUID range bounds');
}
return {
contains(uuid: string): boolean {
return validate(uuid) && uuid >= start && uuid <= end;
},
toString(): string {
return `${start}..${end}`;
}
};
}
// Legacy v1 to modern v6 migration
function modernizeTimestampUuids(uuids: string[]): {
converted: string[],
unchanged: string[],
invalid: string[]
} {
const result = {
converted: [] as string[],
unchanged: [] as string[],
invalid: [] as string[]
};
for (const uuid of uuids) {
if (!validate(uuid)) {
result.invalid.push(uuid);
continue;
}
if (version(uuid) === 1) {
result.converted.push(v1ToV6(uuid));
} else {
result.unchanged.push(uuid);
}
}
return result;
}Install with Tessl CLI
npx tessl i tessl/npm-uuid