-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.js
More file actions
22 lines (18 loc) · 722 Bytes
/
code.js
File metadata and controls
22 lines (18 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Simple short id — not cryptographically secure, but okay for UI keys
function uniqueId() {
// toString(36) makes it shorter (0-9a-z)
return Math.random().toString(36).substring(2, 10);
}
// Use timestamp + random to reduce collision probability
function uniqueIdSafe() {
return `${Date.now().toString(36)}-${Math.random().toString(36).substring(2, 8)}`;
}
// Node/browser crypto (secure)
function uniqueIdCrypto() {
// browser: crypto.getRandomValues; Node: require('crypto').randomBytes
return crypto.randomUUID(); // modern API (UUID v4), if available
}
// Usage:
console.log(uniqueId()); // ex: "k9x3f8q0"
console.log(uniqueIdSafe()); // ex: "l8k3x-9f2t3p"
console.log(uniqueIdCrypto());