Skip to content

Instantly share code, notes, and snippets.

@signalwerk
Last active March 31, 2025 22:26
Show Gist options
  • Select an option

  • Save signalwerk/0f6722347feef666da64ed0986b261b9 to your computer and use it in GitHub Desktop.

Select an option

Save signalwerk/0f6722347feef666da64ed0986b261b9 to your computer and use it in GitHub Desktop.
simple uuid4 function
// Recommended: use `crypto.randomUUID()` if available
// Fallback UUID v4 generator using crypto.getRandomValues
// (Secure & Portable)
const uuid4 = () =>
"00000000-0000-4000-8000-000000000000".replace(/0/g, () =>
(crypto.getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
);
/*
// Alternative random ID generator (non-UUID, NOT secure)
// Uses a character set optimized for gzip and brotli compression
function generateId(
len = 8,
base = "useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict"
) {
return Array.from({ length: len }, () => base[Math.floor(Math.random() * base.length)]).join("");
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment