Created
January 6, 2021 14:22
-
-
Save breadthe/9a2aca061d8cbf88c5a672ac9b11c891 to your computer and use it in GitHub Desktop.
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
// @see https://stackoverflow.com/a/8809472 | |
export function uuid() { // Public Domain/MIT | |
var d = new Date().getTime();//Timestamp | |
var d2 = (performance && performance.now && (performance.now() * 1000)) || 0;//Time in microseconds since page-load or 0 if unsupported | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | |
var r = Math.random() * 16;//random number between 0 and 16 | |
if (d > 0) {//Use timestamp until depleted | |
r = (d + r) % 16 | 0; | |
d = Math.floor(d / 16); | |
} else {//Use microseconds since page-load if supported | |
r = (d2 + r) % 16 | 0; | |
d2 = Math.floor(d2 / 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