uuid
Generates a UUID v4 (Universally Unique Identifier). Creates a random UUID using cryptographically strong random values when available (crypto.randomUUID or crypto.getRandomValues), falling back to Math.random() if needed. Follows RFC 4122 version 4 format.
Installation
npx fragmen add crypto/uuidSource Code
/**
* Generates a UUID v4 (Universally Unique Identifier).
*
* Creates a random UUID using cryptographically strong random values
* when available (crypto.randomUUID or crypto.getRandomValues), falling
* back to Math.random() if needed. Follows RFC 4122 version 4 format.
*
* @tags crypto, pure
* @returns {string} A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
*
* @example
* ```typescript
* const id1 = uuid();
* // '550e8400-e29b-41d4-a716-446655440000'
*
* const id2 = uuid();
* // 'c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd'
*
* // Each call generates a unique identifier
* const ids = Array.from({ length: 100 }, () => uuid());
* const uniqueIds = new Set(ids);
* console.log(uniqueIds.size === 100); // true
* ```
*/
export function uuid(): string {
// Use native crypto.randomUUID if available (Node 16.7.0+, modern browsers)
if (
typeof crypto !== 'undefined' &&
'randomUUID' in crypto &&
typeof crypto.randomUUID === 'function'
) {
return crypto.randomUUID();
}
// Fallback: Use crypto.getRandomValues if available
if (
typeof crypto !== 'undefined' &&
'getRandomValues' in crypto &&
typeof (crypto as Crypto).getRandomValues === 'function'
) {
const bytes = (crypto as Crypto).getRandomValues(new Uint8Array(16));
// Set version (4) and variant bits
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
const hex = Array.from(bytes, (byte: number) =>
byte.toString(16).padStart(2, '0')
).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
// Last resort: Math.random() based UUID (less secure, but works everywhere)
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
Examples
const id1 = uuid();
// '550e8400-e29b-41d4-a716-446655440000'
const id2 = uuid();
// 'c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd'
// Each call generates a unique identifier
const ids = Array.from({ length: 100 }, () => uuid());
const uniqueIds = new Set(ids);
console.log(uniqueIds.size === 100); // true
Related Utilities
random-string
cryptoGenerates a random string of specified length. Creates a cryptographically secure random string using the specified character set. By default uses alphanumeric characters (a-z, A-Z, 0-9). Useful for generating tokens, IDs, or passwords.
chunk
arraySplits an array into chunks of a specified size. Creates a new array containing subarrays (chunks) of the original array, each with a maximum length of the specified size. The last chunk may contain fewer elements if the array length is not evenly divisible by the chunk size.
compact
arrayRemoves falsy values from an array. Creates a new array with all falsy values removed. Falsy values are: false, null, 0, "", undefined, and NaN. This is useful for cleaning arrays and ensuring only truthy values remain.
difference
arrayCreates an array of values from the first array that are not present in the other arrays. Returns a new array containing elements that exist in the first array but not in any of the subsequent arrays. The order of elements follows the order of the first array. Duplicates in the first array are preserved unless they appear in other arrays.
flatten
arrayFlattens nested arrays to a specified depth. Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. A depth of 1 flattens only the first level of nesting, while Infinity flattens all levels.
group-by
arrayGroups the elements of an array based on the result of a callback function. Creates an object where each key represents a group and the value is an array of items that belong to that group. The grouping is determined by the callback function which is applied to each element.