UNPKG

sat-utils

Version:
194 lines 7.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRandomString = getRandomString; exports.getRandomSubString = getRandomSubString; exports.getRandomArrayItem = getRandomArrayItem; exports.getUniqItems = getUniqItems; exports.getNotUniqItems = getNotUniqItems; /* eslint-disable no-console */ /* eslint-disable unicorn/no-object-as-default-parameter, unicorn/consistent-function-scoping*/ const types_1 = require("./types"); const utils_1 = require("./utils"); /** * Get a random substring of a given string. * * @param {string} str - The input string. * @param {number} length - The length of the random substring. * @returns {string} The random substring. * @throws {TypeError} If the input arguments are not of the expected types. */ function getRandomSubString(str, length) { if (!(0, types_1.isString)(str)) { throw new TypeError(`getRandomSubString(): first argument should be a string, current arg is ${(0, types_1.getType)(str)}`); } if (!(0, types_1.isNumber)(length)) { throw new TypeError(`getRandomSubString(): second argument should be a number, current arg is ${(0, types_1.getType)(length)}`); } return Array.from({ length }) .map(() => str.charAt(Math.floor(Math.random() * str.length))) .join(''); } /** * Options for generating a random string. * * @typedef {Object} IOptions * @property {boolean} [numbers] - Include numbers. * @property {boolean} [letters] - Include letters. * @property {boolean} [lettersAndNumbers] - Include letters and numbers. * @property {boolean} [symbols] - Include symbols. * @property {boolean} [lettersNumbersAndSymbols] - Include letters, numbers, and symbols. * @property {boolean} [lowerCase] - Convert the result to lowercase. */ /** * Get a random string of a specified length and options. * * @param {number} length - The length of the random string. * @param {IOptions} [opts] - Options for generating the random string. * @returns {string} The random string. * @throws {TypeError} If the input arguments are not of the expected types. */ function getRandomString(length, opts = { letters: true }) { const allowedOptions = new Set(['numbers', 'letters', 'lettersAndNumbers', 'symbols', 'lettersNumbersAndSymbols']); const thowOptsError = () => { throw new Error(`getRandomString(): second argument should be an object with next opts numbers?: boolean; lettersAndNumbers?: boolean; symbols?: boolean; lettersNumbersAndSymbols?: boolean; lowerCase?: boolean; `); }; if (!(0, types_1.isObject)(opts)) { thowOptsError(); } const options = { ...opts }; if (!Object.keys(options).some(k => allowedOptions.has(k) && options[k])) { options['letters'] = true; } const l = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const s = '!@#$%^&*(((()))_+~?>"|\\}{[]'; const n = '01234567890'; const ln = l + n; const lns = l + s + n; const data = { letters: l, numbers: n, lettersAndNumbers: ln, symbols: s, lettersNumbersAndSymbols: lns, }; if (!(0, types_1.isNumber)(length)) { throw new Error(`getRandomString(): first argument should be a number, current arg is ${(0, types_1.getType)(length)}`); } const { lowerCase, ...restOpts } = options; const optsKeys = Object.keys(restOpts); if (!optsKeys.length || !data[optsKeys[0]]) { thowOptsError(); } const charsKey = optsKeys[0] || 'letters'; const randomStr = getRandomSubString(data[charsKey], length); return lowerCase ? randomStr.toLowerCase() : randomStr; } function getRandomArrayItem(itemsList, quaintity) { if ((0, types_1.isUndefined)(quaintity)) { quaintity = 1; } if (!Array.isArray(itemsList)) { throw new TypeError(`getRandomArrayItem(): first argument should be an array, current arg is ${(0, types_1.getType)(itemsList)}`); } if (!itemsList.length) { throw new RangeError(`getRandomArrayItem(): given array is empty`); } if (quaintity > itemsList.length) { throw new RangeError(`getRandomArrayItem(): more elements taken: ${quaintity} than exist within the given array. Array length ${itemsList.length}`); } if (quaintity === 1) { return itemsList[Math.floor(Math.random() * itemsList.length)]; } return (0, utils_1.shuffleArr)(itemsList).slice(0, quaintity); } /** * Retrieves unique items from the given array based on specified fields. * @template T - The type of items in the array. * @param {T[]} itemsList - The array of items to be processed. * @param {symbol | string | string[]} [uniqByFields] - Optional. The field(s) based on which uniqueness is determined. * @returns {T[]} The array containing unique items based on the specified fields. * @throws {TypeError} If the first argument is not an array. */ function getUniqItems(itemsList, uniqByFields) { if (!Array.isArray(itemsList)) { throw new TypeError(`getUniqItems(): first argument should be an array`); } if (itemsList.length === 0) { return []; } if (itemsList.every(el => (0, types_1.isPrimitive)(el))) { return Array.from(new Set(itemsList)); } if (uniqByFields) { const fields = (0, utils_1.toArray)(uniqByFields); const uniqItems = []; for (const item of itemsList) { if (uniqItems.length === 0) { uniqItems.push(item); } else { const isUniq = uniqItems.every(uniqItem => { return fields.some(field => { if (typeof field === 'string' || typeof field === 'symbol') { return uniqItem[field] !== item[field]; } else if (typeof field === 'function') { return field(Array.from(uniqItems), item); } }); }); if (isUniq) { uniqItems.push(item); } } } return uniqItems; } console.warn(`getUniqItems(): uniqByFields is not provided, returning original array without filtering`); return (0, utils_1.toArray)(itemsList); } /** * Retrieves unique items from the given array based on specified fields. * @template T - The type of items in the array. * @param {T[]} itemsList - The array of items to be processed. * @param {symbol | string | string[]} [uniqByFields] - Optional. The field(s) based on which uniqueness is determined. * @returns {T[]} The array containing unique items based on the specified fields. * @throws {TypeError} If the first argument is not an array. */ function getNotUniqItems(itemsList, uniqByFields) { const seen = new Map(); const duplicates = []; if (!Array.isArray(itemsList)) { throw new TypeError(`getNotUniqItems(): first argument should be an array`); } if (itemsList.length === 0) { return []; } if (itemsList.every(el => (0, types_1.isPrimitive)(el))) { return itemsList.filter((item, index, self) => self.indexOf(item) !== index); } for (const item of itemsList) { const key = (0, utils_1.toArray)(uniqByFields) .map(k => String(item[k])) .join('|'); // Unique composite key if (seen.has(key)) { seen.get(key).push(item); } else { seen.set(key, [item]); } } for (const group of seen.values()) { if (group.length > 1) { duplicates.push(...group); } } return duplicates; } //# sourceMappingURL=randomizer.js.map