Skip to content

Instantly share code, notes, and snippets.

@victor141516
Created May 2, 2025 19:20
Show Gist options
  • Save victor141516/ea49d1457fe97b2b053a19ebafe1e245 to your computer and use it in GitHub Desktop.
Save victor141516/ea49d1457fe97b2b053a19ebafe1e245 to your computer and use it in GitHub Desktop.
Generate UUIDv4
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