UNPKG

sussy-util

Version:
164 lines (163 loc) 6.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const _1 = require("."); const node_crypto_1 = __importDefault(require("node:crypto")); class Random { constructor() { this.defaultCharset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; } /** * It returns a random integer between the lower and upper bounds, inclusive. * * min <= n < max * @param [lower=0] - The lowest number that can be returned. * @param [upper=10] - The upper bound of the random number. * @returns A random integer between the lower and upper bounds. */ randomInt(lower = 0, upper = 10) { lower = Math.floor(lower); upper = Math.ceil(upper); return node_crypto_1.default.randomInt(lower, upper); } /** * Generates a random double (floating-point) number within a specified range. * @param {number} lower - The lower bound of the random number. * @param {number} upper - The upper bound of the random number. * @returns {number} A random double within the specified range. */ randomDouble(lower, upper) { const digits = Math.max(_1.BetterMath.countDecimalDigits(lower), _1.BetterMath.countDecimalDigits(upper)); const multiplier = Math.pow(10, digits); return this.randomInt(lower * multiplier, upper * multiplier) / multiplier; } /** * Returns a random character from a given string or the default charset. * * @param {string} [charset] - The character set to use. Defaults to the default charset. * @returns {string} A random character from the charset. */ randomChar(charset = this.defaultCharset) { return charset.charAt(this.randomInt(0, charset.length)); } /** * Generates a random string of specified length from a given charset or the default charset. * * @param {number} [length=10] - The length of the string to be generated. * @param {string} [charset] - The characters to use when generating the string. Defaults to the default charset. * @returns {string} A string of random characters. */ randomString(length = 10, charset = this.defaultCharset) { let result = ''; for (let i = 0; i < length; i++) { result += this.randomChar(charset); } return result; } /** * It returns a random element from an array. * @param {T[]} arr - T[] - The array to get a random element from. * @returns The return type is T. */ randomElement(arr) { return _1.IsSomething.isArray(arr) ? arr[this.randomInt(0, arr.length)] : arr; } /** * It returns a random element from an array. * @param {T[]} arr - The array to choose a random element from. * @param {number} start - The start index of the range. * @param {number} end - The index of the last element in the range. * @returns The random element in the array. */ randomElementInRange(arr, start, end) { return arr[this.randomInt(start, end)]; } /** * Generates a random element from an object by selecting a random property. * @param {T} obj - The object to choose a random property from. * @returns The value of a random property in the object. */ randomProperty(obj) { const keys = Object.keys(obj); const randomKey = keys[this.randomInt(0, keys.length)]; return obj[randomKey]; } /** * Generates a random boolean value. * @returns A random boolean value. */ randomBoolean() { return this.randomInt(0, 2) == 0; } /** * Generates a random color in hexadecimal format (#RRGGBB). * * @returns {string} A random color in hexadecimal format. */ randomColor() { const r = this.randomInt(0, 256).toString(16).padStart(2, '0'); const g = this.randomInt(0, 256).toString(16).padStart(2, '0'); const b = this.randomInt(0, 256).toString(16).padStart(2, '0'); return `#${r}${g}${b}`; } /** * Generates a random RGB color array [r, g, b]. * * @returns {number[]} A random RGB color array. */ randomRgbColor() { return [this.randomInt(0, 256), this.randomInt(0, 256), this.randomInt(0, 256)]; } /** * Generates a random UUID (Universally Unique Identifier). * * @returns {string} A random UUID. */ randomUUID() { const segments = []; for (let i = 0; i < 8; i++) { segments.push(this.randomString(4, '0123456789abcdef')); } return segments.join('-'); } /** * Generates a random date within a specified range. * * @param {Date} startDate - The start date of the range. * @param {Date} endDate - The end date of the range. * @returns {Date} A random date within the specified range. */ randomDate(startDate, endDate) { const startMillis = startDate.getTime(); const endMillis = endDate.getTime(); const randomMillis = this.randomInt(startMillis, endMillis + 1); return new Date(randomMillis); } static getInstance() { return this.instance; } /** * Returns a random value from an enum. * @param {T} enumObject - The enum to choose a random value from. * @returns The random value from the enum. */ randomEnumValue(enumObject) { const values = Object.values(enumObject); return values[this.randomInt(0, values.length)]; } /** * Generates a random date in the past within a specified range. * @param {number} yearsAgo - The number of years ago the date can be. * @returns {Date} A random date in the past within the specified range. */ randomDateInPast(yearsAgo) { const today = new Date(); const startYear = today.getFullYear() - yearsAgo; const startDate = new Date(startYear, 0, 1); return this.randomDate(startDate, today); } } Random.instance = new Random(); exports.default = Random.getInstance();