Created
January 20, 2021 17:10
-
-
Save james2doyle/0b6d9cbee9e7b458d3b56525971c890b to your computer and use it in GitHub Desktop.
Create a UUID using window.performance or Date.now
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
/** | |
* Create a UUID | |
* @see http://stackoverflow.com/a/8809472/188246 | |
* | |
* @return {string} | |
*/ | |
export default function makeUuid(): string { | |
// your favourite guid generation function could go here | |
// ex: http://stackoverflow.com/a/8809472/188246 | |
let d = new Date().getTime() + (window.performance || Date).now(); | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | |
const r = (d + Math.random() * 16) % 16 | 0; | |
d = Math.floor(d / 16); | |
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