Created
August 23, 2021 07:12
-
-
Save TobidieTopfpflanze/0761fcc45bb05752cfd1c91f9cb550c8 to your computer and use it in GitHub Desktop.
Generate UUID v4
This file contains 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 generateUUID(): string { | |
let dt = Date.now() | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | |
const r = Math.trunc((dt + Math.random() * 16) % 16) | |
dt = Math.floor(dt / 16) | |
// eslint-disable-next-line no-bitwise | |
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment