@kikiutils/node
Version:
A modular utility library for Node.js offering secure hashing, flexible logging, datetime manipulation, and more.
31 lines (28 loc) • 1.39 kB
JavaScript
;
/**
* Generates a value using a provided generator function, where the input length
* is determined by two levels of nested random ranges:
*
* 1. First, a random number (`innerMin`) is chosen between `minMin` and `minMax`.
* 2. Then, a final length is chosen between `Math.max(innerMin, maxMin)` and `maxMax`.
* 3. The generator is called with the final length and its result is returned.
*
* This function supports any return type by using a generic type parameter.
*
* @typeParam T - The return type of the generator function.
*
* @param generator - A function that accepts a length and returns a value of type T.
* @param minMin - Lower bound of the first random range.
* @param minMax - Upper bound of the first random range.
* @param maxMin - Lower bound of the second random range.
* @param maxMax - Upper bound of the second random range.
* @returns The result of the generator function using the computed final length.
*/
function generateWithNestedRandomLength(generator, minMin, minMax, maxMin, maxMax) {
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const innerMin = random(minMin, minMax);
const finalLength = random(Math.max(innerMin, maxMin), maxMax);
return generator(finalLength);
}
exports.generateWithNestedRandomLength = generateWithNestedRandomLength;
//# sourceMappingURL=random.cjs.map