@amxdev/format-bytes
Version:
Format bytes as human-readable string (e.g. 2.5 MB).
17 lines (15 loc) • 514 B
text/typescript
export function uuid(): string {
const hex = [...crypto.getRandomValues(new Uint8Array(16))].map(b =>
b.toString(16).padStart(2, '0')
);
// Per RFC4122: version 4 UUID format
hex[6] = (parseInt(hex[6], 16) & 0x0f | 0x40).toString(16); // version 4
hex[8] = (parseInt(hex[8], 16) & 0x3f | 0x80).toString(16); // variant
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('-');
}