quick-random
Version:
A random generator for JS and TS
221 lines (219 loc) • 7.28 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var allCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var random = {
/**
* Generate a random number between min and max
* @param {number} min the minimum number to generate
* @param {number} max the maximum number to generate
* @returns {number} a random number between min and max
* @see random.bigint for a bigint version
* @example
* random.number(0, 100) // 50
* random.number(0.5, 1.5) // 1.2
*/
number: (min, max) => {
if (Number.isInteger(min) && Number.isInteger(max)) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
return Math.random() * (max - min) + min;
},
/**
* Generate a random string
* @param {number} length the length of the string
* @param {string} characters the characters to generate the string from (default: allCharacters)
* @returns {string} a random string
* @example
* random.string(10) // 'aBcDeFgHiJ'
* random.string(10, {includeUpperCase: false}) // 'abcdefghij'
*/
string: (length, characters = allCharacters) => {
let result = "";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(random.number(0, charactersLength - 1));
}
return result;
},
/**
* @returns {string} a random character
* @example
* random.char(); //returns 'A'
* random.char(); //returns 'b'
*/
char: () => random.string(1),
/**
* Generate a random boolean
* @returns {boolean} a random boolean
* @example
* random.boolean() // true
* random.boolean() // false
*/
boolean: () => {
return Math.random() >= 0.5;
},
/**
* Generates a random date between minDate and maxDate
* @param {Date} minData optional minimum date
* @param {Date maxDate optional maximum date
* @returns {Date} a random date
*
* @example
* random.date() // 2021-08-01T12:00:00.000Z
* random.date(new Date('2021-08-01T12:00:00.000Z'), new Date('2021-08-01T12:00:00.000Z')) // 2021-08-01T12:00:00.000Z
*/
date: (minData, maxDate) => {
const min = minData ? minData.getTime() : 0;
const max = maxDate ? maxDate.getTime() : Date.now() + 1e11;
return new Date(random.number(min, max));
},
/**
*
* @returns {symbol} a random symbol
* @example
* random.symbol() // Symbol(5)
* random.symbol() // Symbol(10)
*/
symbol: () => Symbol(random.string(10)),
/**
* Generates a random bigint
* @param {bigint} min minimum number
* @param {bigint} max maximum number
* @returns {bigint} a random bigint
* @see random.number for a number version
* @example
* random.bigint(0n, 100n) // 50n
* random.bigint(0n, 100n) // 10n
*/
bigint: (min, max) => {
return BigInt(random.number(Number(min), Number(max)));
},
/**
* Generate a random array
*
* Can generate an array of numbers, strings, booleans, symbols, dates and bigints
* @param {number} length the length of the array
* @param {T} type the type of the array
* @returns {T[]} a random array
* @example
* random.array(10, 'number') // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
* random.array(10, 'string') // [ 'aBcDeFgHiJ', 'tUvWxYzAbC', 'dEfGhIjKlM', 'nOpQrStUvW', 'xYzAbCdEfG', 'hIjKlMnOpQ', 'rStUvWxYzA', 'bCdEfGhIjK', 'lMnOpQrStU', 'vWxYzAbCdE' ]
* random.array(10, 'boolean') // [ true, false, true, false, true, false, true, false, true, false ]
* random.array(10, 'symbol') // [ Symbol
*/
array: (length, type) => {
const result = [];
for (let i = 0; i < length; i++) {
switch (type) {
case "number":
result.push(random.number(0, 100));
break;
case "string":
result.push(random.string(random.number(0, 10)));
break;
case "boolean":
result.push(random.boolean());
break;
case "symbol":
result.push(random.symbol());
break;
case "date":
result.push(random.date());
break;
case "bigint":
result.push(random.bigint(0n, 100n));
break;
default:
throw new Error("Invalid type");
}
}
return result;
},
/**
* Get a random element from an array
* @param {T[]} array the array to get the element from
* @returns {T} A random element from the array
*/
elemtFromArray: (array) => array[random.number(0, array.length)],
/**
* Get a random key from an object
* @param {T extends object} object
* @returns {keyof T} a random key from the object
*/
keyFromObject: (object) => {
const keys = Object.keys(object);
return keys[random.number(0, keys.length - 1)];
},
/**
* Gets a random value from an object
* @param {T} object the object that the value would be taken from
* @returns {T[keyof T]} random value from the object
*/
valueFromObject: (object) => {
const values = Object.values(object);
return values[random.number(0, values.length - 1)];
},
/**
* `Still experimental`
*
* Generates a random object based on the object passed
*
* Important to note that the symbol type is not supported at the moment
* @param {T} object a object to generate a random object from
* @returns {T} a random object of the same type as the object passed
* @example
* random.object({a: 1, b: 'hello', c: {}}) // { a: 50, b: 'aBcDeFgHiJ', c: {} }
*
*/
object: (object) => {
const randomObject = structuredClone(object);
Object.keys(object).forEach((key) => {
const element = object[key];
switch (typeof element) {
case "number":
randomObject[key] = random.number(0, 100);
break;
case "string":
randomObject[key] = random.string(random.number(0, 10));
break;
case "boolean":
randomObject[key] = random.boolean();
break;
case "symbol":
throw new Error("Symbol is not supported at the moment");
case "bigint":
randomObject[key] = random.bigint(0n, 100n);
break;
case "object":
randomObject[key] = random.object(element);
break;
default:
throw new Error("Invalid type");
}
});
return randomObject;
}
};
var src_default = random;