Last active
March 31, 2025 22:26
-
-
Save signalwerk/0f6722347feef666da64ed0986b261b9 to your computer and use it in GitHub Desktop.
simple uuid4 function
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
| // 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