唯一标识符UUID类的实现
因为用不到了,记录下
/**
* UUID 工具类
*/
class UUIDUtils {
/**
* 生成一个UUID
*/
static generate(): string {
let uuid: string | null = null;
if (typeof crypto === "object") {
if (typeof crypto.randomUUID === "function") {
uuid = crypto.randomUUID();
} else if (
typeof crypto.getRandomValues === "function" &&
typeof Uint8Array === "function"
) {
const callback = (c: string) => {
const num = Number(c);
return (
num ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))
).toString(16);
};
uuid = "10000000-1000-4000-8000-100000000000".replace(
/[018]/g,
callback
);
}
}
if (!uuid) {
let timestamp = new Date().getTime();
let perforNow =
(typeof performance !== "undefined" &&
performance.now &&
performance.now() * 1000) ||
0;
uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
let random = Math.random() * 16;
if (timestamp > 0) {
random = (timestamp + random) % 16 | 0;
timestamp = Math.floor(timestamp / 16);
} else {
random = (perforNow + random) % 16 | 0;
perforNow = Math.floor(perforNow / 16);
}
return (c === "x" ? random : (random & 0x3) | 0x8).toString(16);
});
}
return uuid;
}
}
浙公网安备 33010602011771号