/** * Generate a short random identifier suitable for ephemeral DOM keys, * chat message IDs, and form row keys. * * Prefers `crypto.randomUUID()` when available (modern browsers, Node 19+). * Falls back to `Math.random()` so the helper still works in older runtimes * such as JSDOM-based tests where the crypto.subtle API is partially * polyfilled but `randomUUID` may be missing. */ export function newId(prefix = "id"): string { if (typeof crypto !== "undefined" && "randomUUID" in crypto) { return crypto.randomUUID(); } return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2)}`; }