Last active
March 2, 2020 09:58
-
-
Save jorgeortega/01771e65ed3c9fe548c145ca27e1d5ae to your computer and use it in GitHub Desktop.
Generate random uuid
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
/* | |
* 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