hd-utils
Version:
A handy utils for modern JS developers
27 lines (26 loc) • 1.01 kB
JavaScript
/**
* @description Will generate a similar v4 UUID
* if you need more robust solution for uuid, pls check https://github.com/uuidjs/uuid
* @example generateUUID() => "741baba3-1efc-b52e-5d9c-75b1fc80395a"
*/
export default function generateUUID() {
var d = new Date().getTime(); //Timestamp
var d2 = (typeof performance !== 'undefined' &&
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);
});
}