返回 JoyAI-Echo
utils.ts
1 import { clsx, type ClassValue } from "clsx";
2 import { twMerge } from "tailwind-merge";
3
4 export function cn(...inputs: ClassValue[]): string {
5 return twMerge(clsx(inputs));
6 }
7
8 export function randomId(): string {
9 const cryptoApi = globalThis.crypto;
10 if (typeof cryptoApi?.randomUUID === "function") {
11 return cryptoApi.randomUUID();
12 }
13
14 const bytes = new Uint8Array(16);
15 if (typeof cryptoApi?.getRandomValues === "function") {
16 cryptoApi.getRandomValues(bytes);
17 } else {
18 for (let i = 0; i < bytes.length; i += 1) {
19 bytes[i] = Math.floor(Math.random() * 256);
20 }
21 }
22
23 bytes[6] = (bytes[6] & 0x0f) | 0x40;
24 bytes[8] = (bytes[8] & 0x3f) | 0x80;
25
26 const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));
27 return [
28 hex.slice(0, 4).join(""),
29 hex.slice(4, 6).join(""),
30 hex.slice(6, 8).join(""),
31 hex.slice(8, 10).join(""),
32 hex.slice(10, 16).join(""),
33 ].join("-");
34 }
35
35 lines TYPESCRIPT