Extracting Largest Segment from UUID for Brevity
UUIDs are long and unwieldy for filenames. Extract the largest segment for shorter, readable names.
Problem
Full UUID: a3b4c5d6-e7f8-4a5b-9c0d-1e2f3a4b5c6d (36 characters)
Filename: a3b4c5d6-e7f8-4a5b-9c0d-1e2f3a4b5c6d.png ❌ Too long
Solution: Extract Largest Segment
function extractLargestUuidSegment(uuid: string): string {
// Remove dashes and split into segments
const withoutDashes = uuid.replace(/-/g, '');
const segments = [
withoutDashes.slice(0, 8), // First 8 chars
withoutDashes.slice(8, 12), // Next 4 chars
withoutDashes.slice(12, 16), // Next 4 chars
withoutDashes.slice(16, 20), // Next 4 chars
withoutDashes.slice(20), // Remaining 12 chars
];
// Find the longest segment (last one is typically longest)
return segments.reduce((longest, current) =>
current.length > longest.length ? current : longest
);
}
Simpler Approach
The last segment of a UUID (after removing dashes) is always 12 characters:
function extractLargestUuidSegment(uuid: string): string {
return uuid.replace(/-/g, '').slice(-12);
}
Examples
| UUID | Largest Segment |
|---|---|
a3b4c5d6-e7f8-4a5b-9c0d-1e2f3a4b5c6d |
1e2f3a4b5c6d |
12345678-1234-1234-1234-123456789abc |
123456789abc |
Filename Usage
import { randomUUID } from 'crypto';
function generateAssetFilename(extension: string): string {
const uuid = randomUUID();
const segment = extractLargestUuidSegment(uuid);
return `${segment}.${extension}`;
}
// Examples:
// "1e2f3a4b5c6d.png"
// "123456789abc.wav"
Comparison
| Method | Length | Uniqueness |
|---|---|---|
| Full UUID | 36 chars | Cryptographically unique |
| Last 12 chars | 12 chars | Practically unique (16^12 = 2.8×10^14 combos) |
| Last 8 chars | 8 chars | Low (16^8 = 4.3×10^9 combos) |
When to Use
| Scenario | Recommended |
|---|---|
| Asset filenames (images, audio) | 12 chars |
| Temporary files | 8-12 chars |
| Database IDs | Full UUID |
| API tokens | Full UUID |
Safety Considerations
For 12-character segments:
- Collision probability: ~1 in 2.8×10^14
- Safe for up to ~10 million assets with negligible collision risk
- For billions of assets, use full UUID or collision detection
Complete Utility
class UuidHelper {
/**
* Extract largest segment (last 12 chars) from UUID
*/
static extractLargestSegment(uuid: string): string {
return uuid.replace(/-/g, '').slice(-12);
}
/**
* Generate short UUID (8 characters)
*/
static generateShort(): string {
const uuid = randomUUID();
return uuid.replace(/-/g, '').slice(0, 8);
}
/**
* Generate asset filename with short UUID
*/
static generateAssetFilename(extension: string): string {
const segment = this.extractLargestSegment(randomUUID());
return `${segment}.${extension}`;
}
/**
* Check if string is a valid UUID
*/
static isValidUuid(str: string): boolean {
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return uuidRegex.test(str);
}
}
Usage Examples
// Asset storage
const imageId = UuidHelper.extractLargestUuid(randomUUID());
await saveImage(buffer, `${imageId}.png`);
// Reference in code
const scene = {
imageUuid: '1e2f3a4b5c6d', // Short segment
audioUuid: '123456789abc',
};
// Full path reconstruction
const imagePath = `public/${scene.imageUuid}.png`;