From cb4587de75e3d8a600393ef9df522180030884a2 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 15 Nov 2025 02:09:01 +0000 Subject: [PATCH] completionId: cgen-78ac2f9f459f46b795596185094eeec0 cgen-78ac2f9f459f46b795596185094eeec0 --- client/lib/utils.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client/lib/utils.ts b/client/lib/utils.ts index a5ef1935..2a5c685b 100644 --- a/client/lib/utils.ts +++ b/client/lib/utils.ts @@ -4,3 +4,33 @@ import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } + +/** + * Generate a UUID v4 using the Web Crypto API. + * Works in both browser and Node.js environments. + */ +export function generateUUID(): string { + // Use crypto.randomUUID() if available (modern browsers and Node.js 15.7+) + if (typeof crypto !== "undefined" && crypto.randomUUID) { + return crypto.randomUUID(); + } + + // Fallback for older environments + const array = new Uint8Array(16); + crypto.getRandomValues(array); + + // Set version to 4 (random) + array[6] = (array[6] & 0x0f) | 0x40; + // Set variant to RFC 4122 + array[8] = (array[8] & 0x3f) | 0x80; + + const hex = Array.from(array).map((b) => b.toString(16).padStart(2, "0")); + + return [ + hex.slice(0, 4).join(""), + hex.slice(4, 6).join(""), + hex.slice(6, 8).join(""), + hex.slice(8, 10).join(""), + hex.slice(10, 16).join(""), + ].join("-"); +}