super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
87 lines (86 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.random = random;
exports.randomInt = randomInt;
exports.randomString = randomString;
exports.randomUUID = randomUUID;
/**
* Generates a random number between min and max (inclusive).
*
* @param min - The minimum value
* @param max - The maximum value
* @returns A random number between min and max
*
* @example
* ```ts
* random(1, 10);
* // => a number between 1 and 10
* ```
*/
function random(min = 0, max = 1) {
return Math.random() * (max - min) + min;
}
/**
* Generates a random integer between min and max (inclusive).
*
* @param min - The minimum value
* @param max - The maximum value
* @returns A random integer between min and max
*
* @example
* ```ts
* randomInt(1, 10);
* // => an integer between 1 and 10
* ```
*/
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Generates a random string of the specified length.
*
* @param length - The length of the string
* @param charset - The characters to use (defaults to alphanumeric)
* @returns A random string
*
* @example
* ```ts
* randomString(10);
* // => "a1b2c3d4e5"
*
* randomString(5, 'ABC');
* // => "BACAB"
* ```
*/
function randomString(length, charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
let result = '';
for (let i = 0; i < length; i++) {
result += charset.charAt(Math.floor(Math.random() * charset.length));
}
return result;
}
/**
* Generates a random UUID v4.
*
* @returns A random UUID v4 string
*
* @example
* ```ts
* randomUUID();
* // => "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
* ```
*/
function randomUUID() {
// Use the built-in crypto.randomUUID if available (Node.js 16+)
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Otherwise, implement a simple UUID v4 generator
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);
});
}