mirror of
https://github.com/zhigang1992/now-deployment.git
synced 2026-04-22 19:48:14 +08:00
20 lines
465 B
TypeScript
20 lines
465 B
TypeScript
/** Generates UUID v4 */
|
|
|
|
export default function uuid() {
|
|
function s(n: number) {
|
|
return h((Math.random() * (1 << (n << 2))) ^ Date.now()).slice(-n);
|
|
}
|
|
function h(n: number) {
|
|
return (n | 0).toString(16);
|
|
}
|
|
return [
|
|
s(4) + s(4),
|
|
s(4),
|
|
`4${s(3)}`, // UUID version 4
|
|
h(8 | (Math.random() * 4)) + s(3), // {8|9|A|B}xxx
|
|
Date.now()
|
|
.toString(16)
|
|
.slice(-10) + s(2) // Use timestamp to avoid collisions
|
|
].join('-');
|
|
}
|