@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
161 lines (160 loc) • 6.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Affirm_1 = __importDefault(require("./Affirm"));
/**
* Core Algorithms that are shared between different project/problems.
* This file needs to be as lean as possible and must not include any external dependencies.
*/
const algo = {
hasVal: (value) => value !== null && value !== undefined,
sleep: (milliseconds) => {
(0, Affirm_1.default)(milliseconds > 0, 'Invalid @milliseconds parameter. Must be a positive number.');
return new Promise(resolve => setTimeout(resolve, milliseconds));
},
rng: (min, max) => {
(0, Affirm_1.default)(min, 'Invalid @min parameter. Must be a valid number.');
(0, Affirm_1.default)(max, 'Invalid @max parameter. Must be a valid number.');
(0, Affirm_1.default)(min < max, 'The minimum value must be less than the maximum value.');
return Math.floor(Math.random() * (max - min + 1)) + min;
},
uniqBy: (array, pk) => {
if (!array || array.length === 0)
return [];
return Array.from(new Set(array.map(x => x[pk])));
},
uniq: (array) => {
if (!array || array.length === 0)
return [];
return Array.from(new Set(array));
},
duplicatesObject: (array, pk) => {
if (!array || array.length === 0)
return [];
return array.filter(x => array.filter(k => k[pk] === x[pk]).length > 1);
},
duplicates: (array) => {
if (!array || array.length === 0)
return [];
return array.filter(x => array.filter(k => k === x).length > 1);
},
locations: (string, substring) => {
(0, Affirm_1.default)(string, 'Invalid @string parameter.');
(0, Affirm_1.default)(substring, 'Invalid @substring parameter.');
const a = [];
let i = -1;
while ((i = string.indexOf(substring, i + 1)) >= 0)
a.push(i);
return a;
},
isNumber: (value) => typeof value === 'number',
orderBy: (list, key, direction = 'asc') => {
(0, Affirm_1.default)(list, 'Invalid array.');
Affirm_1.default.hasItems(list, 'Array must be non-empty.');
(0, Affirm_1.default)(direction, 'Invalid direction parameter. Must be "asc" or "desc".');
const sortedList = [...list];
if (key) {
sortedList.sort((a, b) => {
const aValue = a[key];
const bValue = b[key];
if (aValue < bValue)
return direction === 'asc' ? -1 : 1;
if (aValue > bValue)
return direction === 'asc' ? 1 : -1;
return 0;
});
return sortedList;
}
else {
sortedList.sort((a, b) => {
if (a < b)
return direction === 'asc' ? -1 : 1;
if (a > b)
return direction === 'asc' ? 1 : -1;
return 0;
});
return sortedList;
}
},
groupBy: (array, key) => {
Affirm_1.default.hasItems(array, 'Array must be non-empty');
(0, Affirm_1.default)(key, 'key is invalid');
const result = new Map();
for (let i = 0; i < array.length; i++) {
const item = array[i];
const keyVal = item[key];
if (result.has(keyVal)) {
const current = result.get(keyVal);
current.push(item);
result.set(keyVal, current);
}
else {
result.set(keyVal, [item]);
}
}
return result;
},
chunkString: (text, chunkSize) => {
(0, Affirm_1.default)(text, 'Invalid text');
(0, Affirm_1.default)(chunkSize, 'Invalid chunk size');
(0, Affirm_1.default)(text.length > 0, 'Text length must be greater than 0');
(0, Affirm_1.default)(chunkSize > 0, 'Chunk size must be greater than 0');
if (text.length <= chunkSize)
return [text];
const numChunks = Math.ceil(text.length / chunkSize);
const chunks = new Array(numChunks);
for (let i = 0; i < chunks.length; i++) {
chunks[i] = text.substring(i * chunkSize, chunkSize);
}
return chunks;
},
first: (arr) => {
(0, Affirm_1.default)(algo.hasVal(arr), 'Array must not be null or undefined');
Affirm_1.default.hasItems(arr, 'Array must be non-empty');
return arr[0];
},
last: (arr) => {
(0, Affirm_1.default)(algo.hasVal(arr), 'Array must not be null or undefined');
(0, Affirm_1.default)(arr.length > 0, 'Array must be non-empty');
return arr[arr.length - 1];
},
mean: (numbers) => {
(0, Affirm_1.default)(algo.hasVal(numbers), 'Array must not be null or undefined');
if (numbers.length === 0)
return 0;
const total = algo.sum(numbers);
return total / numbers.length;
},
sum: (numbers) => {
(0, Affirm_1.default)(algo.hasVal(numbers), 'Array must not be null or undefined');
if (numbers.length === 0)
return 0;
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
},
round: (number, precision) => {
(0, Affirm_1.default)(algo.hasVal(number), 'Number must not be null or undefined');
const factor = Math.pow(10, precision !== null && precision !== void 0 ? precision : 2);
return Math.round(number * factor) / factor;
},
min: (arr) => {
(0, Affirm_1.default)(algo.hasVal(arr), 'Array must not be null or undefined');
if (arr.length === 0)
return 0;
return Math.min(...arr);
},
max: (arr) => {
(0, Affirm_1.default)(algo.hasVal(arr), 'Array must not be null or undefined');
if (arr.length === 0)
return 0;
return Math.max(...arr);
},
replaceAll: (text, search, replace) => text.replace(new RegExp(search, 'g'), replace),
deepClone: (data) => JSON.parse(JSON.stringify(data))
};
exports.default = algo;