Skip to content

Instantly share code, notes, and snippets.

@TobidieTopfpflanze
Created August 23, 2021 07:12
Show Gist options
  • Save TobidieTopfpflanze/0761fcc45bb05752cfd1c91f9cb550c8 to your computer and use it in GitHub Desktop.
Save TobidieTopfpflanze/0761fcc45bb05752cfd1c91f9cb550c8 to your computer and use it in GitHub Desktop.
Generate UUID v4
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