Created
May 2, 2025 19:20
-
-
Save victor141516/ea49d1457fe97b2b053a19ebafe1e245 to your computer and use it in GitHub Desktop.
Generate UUIDv4
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 default () => { | |
const u = [...crypto.getRandomValues(new Uint8Array(16))] | |
u[6] = (u[6] & 0x0f) | 0x40 | |
u[8] = (u[8] & 0x0f) | 0x80 | |
return u | |
.map((b) => b.toString(16).padStart(2, '0')) | |
.join('') | |
.replace(/^(.{8})(.{4})(.{4})(.{4})/, '$1-$2-$3-$4-') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment