Skip to content

Instantly share code, notes, and snippets.

View jorgeortega's full-sized avatar
💭
Learning all the time

Jorge Ortega jorgeortega

💭
Learning all the time
View GitHub Profile
@jorgeortega
jorgeortega / uuid.js
Last active March 2, 2020 09:58
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()}`