Created
August 23, 2025 12:09
-
-
Save frzi/365b1ab84857b6f1a52134066a2607ef to your computer and use it in GitHub Desktop.
Primitive UUID in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function uuid(value?: string): bigint { | |
const uuid = value || crypto.randomUUID() | |
return BigInt('0x' + uuid.replace(/-/g, '')) | |
} | |
export function uuidString(uuid?: bigint): string { | |
if (!uuid) { | |
return crypto.randomUUID() | |
} | |
const str = uuid.toString(16).padStart(32, '0') | |
const g1 = str.substring(0, 8) | |
const g2 = str.substring(8, 12) | |
const g3 = str.substring(12, 16) | |
const g4 = str.substring(16, 20) | |
const g5 = str.substring(20, 32) | |
return `${g1}-${g2}-${g3}-${g4}-${g5}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment