Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created January 20, 2021 17:10
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 james2doyle/0b6d9cbee9e7b458d3b56525971c890b to your computer and use it in GitHub Desktop.
Save james2doyle/0b6d9cbee9e7b458d3b56525971c890b to your computer and use it in GitHub Desktop.
Create a UUID using window.performance or Date.now
/**
* 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