Skip to content

Instantly share code, notes, and snippets.

@jorgeortega
Last active March 2, 2020 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgeortega/01771e65ed3c9fe548c145ca27e1d5ae to your computer and use it in GitHub Desktop.
Save jorgeortega/01771e65ed3c9fe548c145ca27e1d5ae to your computer and use it in GitHub Desktop.
Generate random uuid
/*
* Generates a universally unique identifier based on random values and timestamp.
* Random length can be increased to get even more unique values.
*/
function uuid() {
if (!window.crypto) throw new Error('crypto not available')
const randomLength = 8
const uuidUint8Array = new Uint8Array(randomLength)
window.crypto.getRandomValues(uuidUint8Array)
return `${uuidUint8Array.join('')}${Date.now()}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment