typescriptbeginner
UUID and Nano ID Generator
Generate RFC-4122 v4 UUIDs and URL-safe nano IDs using the Node.js built-in crypto module with zero deps.
typescriptPress ⌘/Ctrl + Shift + C to copy
import crypto from 'crypto';
export function uuid(): string {
return crypto.randomUUID();
}
export function nanoId(size = 21): string {
const alphabet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
const bytes = crypto.randomBytes(size);
let id = '';
for (let i = 0; i < size; i++) {
id += alphabet[bytes[i] & 63];
}
return id;
}
export function prefixedId(prefix: string, size = 12): string {
return `${prefix}_${nanoId(size)}`;
}
// Usage:
// uuid() -> 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
// nanoId() -> 'V1StGXR8_Z5jdHi6B-myT'
// prefixedId('usr') -> 'usr_a1B2c3D4e5F6'Use Cases
- Database primary keys
- Session identifiers
- URL-safe short IDs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Node.js Crypto Utility Functions
Common cryptographic operations: hashing, HMAC, encryption, random tokens, and password hashing.
Best for: Secure password storage and verification
#nodejs#crypto
typescriptintermediate
Crypto Hashing and HMAC
Generate secure hashes, HMACs, and checksums using Node.js built-in crypto module.
Best for: Webhook signature verification
#nodejs#crypto
typescriptbeginner
useCopyToClipboard Hook
Copy text to clipboard with success state and automatic reset timer using the Clipboard API.
Best for: Copy code snippets
#hooks#clipboard
typescriptbeginner
Conditional Wrapper Component
Utility component that conditionally wraps children in a parent element or renders them directly.
Best for: Optional link wrapping
#utility#wrapper