topkat-utils
Version:
A comprehensive collection of TypeScript/JavaScript utility functions for common programming tasks. Includes validation, object manipulation, date handling, string formatting, and more. Zero dependencies, fully typed, and optimized for performance.
126 lines • 5.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.includes = exports.randomItemInArray = exports.isNotEmptyArray = exports.pushIfNotExist = exports.arrayCount = exports.noDuplicateFilter = exports.getArrayDiff = exports.getNotInArrayA = exports.getArrayInCommon = exports.compareArrays = exports.asArray = exports.strAsArray = exports.ensureIsArrayAndPush = exports.shuffleArray = void 0;
//----------------------------------------
// ARRAY UTILS
//----------------------------------------
const object_utils_1 = require("./object-utils");
const isset_1 = require("./isset");
/** Randomize array in place and return the same array than inputed */
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
exports.shuffleArray = shuffleArray;
/**
* Maye sure obj[addr] is an array and push a value to it
* @param {Object} obj parent object
* @param {String} addr field name in parent
* @param {Any} valToPush
* @param {Boolean} onlyUniqueValues default:false; may be true or a comparision function; (a,b) => return true if they are the same like (a, b) => a.name === b.name
* @return obj[addr] eventually processed by the callback
*/
function ensureIsArrayAndPush(obj, addr, valToPush, onlyUniqueValues) {
return (0, object_utils_1.ensureObjectProp)(obj, addr, [], objValue => {
if ((0, isset_1.isset)(onlyUniqueValues)) {
let duplicateFound = false;
if (typeof onlyUniqueValues === 'function')
duplicateFound = objValue.some(a => onlyUniqueValues(a, valToPush));
else
duplicateFound = objValue.includes(valToPush);
if (!duplicateFound)
objValue.push(valToPush);
}
else
objValue.push(valToPush);
});
}
exports.ensureIsArrayAndPush = ensureIsArrayAndPush;
/** If a string is provided, return it as array else return the value */
function strAsArray(arrOrStr) {
return (typeof arrOrStr === 'string' ? [arrOrStr] : arrOrStr);
}
exports.strAsArray = strAsArray;
/** If not an array provided, return the array with the value
* /!\ NOTE /!\ In case the value is null or undefined, it will return that value
*/
function asArray(item, returnValueIfUndefined) {
if (typeof item === 'undefined')
return returnValueIfUndefined;
else
return (Array.isArray(item) ? item : [item]);
}
exports.asArray = asArray;
/** @return {object} { inCommon, notInB, notInA } */
function compareArrays(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
return {
inCommon: getArrayInCommon(arrayA, arrayB, compare),
notInB: getNotInArrayA(arrayB, arrayA, compare),
notInA: getNotInArrayA(arrayA, arrayB, compare),
};
}
exports.compareArrays = compareArrays;
/** @return [] only elements that are both in arrayA and arrayB */
function getArrayInCommon(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
if (!Array.isArray(arrayA) || !Array.isArray(arrayB))
return [];
else
return arrayA.filter(a => arrayB.some(b => compare(a, b)));
}
exports.getArrayInCommon = getArrayInCommon;
/** @return [] only elements that are in arrayB and not in arrayA */
function getNotInArrayA(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
if (!Array.isArray(arrayA) && Array.isArray(arrayB))
return arrayB;
else if (!Array.isArray(arrayB))
return [];
else
return arrayB.filter(b => !arrayA.some(a => compare(a, b)));
}
exports.getNotInArrayA = getNotInArrayA;
/**
* @return [] only elements that are in neither arrayA and arrayB
*/
function getArrayDiff(arrayA = [], arrayB = [], compare = (a, b) => a === b) {
return [...getNotInArrayA(arrayA, arrayB, compare), ...getNotInArrayA(arrayB, arrayA, compare)];
}
exports.getArrayDiff = getArrayDiff;
/** filter duplicate values in an array
* @param {function} comparisonFn default:(a, b) => a === b. A function that shall return true if two values are considered equal
* @return {array|function}
*/
function noDuplicateFilter(arr, comparisonFn = (a, b) => a === b) {
return arr.filter((a, i, arr) => arr.findIndex(b => comparisonFn(a, b)) === i);
}
exports.noDuplicateFilter = noDuplicateFilter;
/** Count number of occurence of item in array */
function arrayCount(item, arr) {
return arr.reduce((total, item2) => item === item2 ? total + 1 : total, 0);
}
exports.arrayCount = arrayCount;
/**
* @param {Function} comparisonFunction default: (itemToPush, itemAlreadyInArray) => itemToPush === itemAlreadyInArray; comparison function to consider the added item duplicate
*/
function pushIfNotExist(arrayToPushInto, valueOrArrayOfValuesToBePushed, comparisonFunction = (a, b) => a === b) {
const valuesToPush = asArray(valueOrArrayOfValuesToBePushed).filter(a => !arrayToPushInto.some(b => comparisonFunction(a, b)));
arrayToPushInto.push(...valuesToPush);
return arrayToPushInto;
}
exports.pushIfNotExist = pushIfNotExist;
function isNotEmptyArray(arr) {
return Array.isArray(arr) && !!arr.length;
}
exports.isNotEmptyArray = isNotEmptyArray;
function randomItemInArray(array) {
return array[Math.floor(Math.random() * array.length)];
}
exports.randomItemInArray = randomItemInArray;
/** This has been made because in typescript the FUCKING TYPE OF THE ITEM must be typê of InputArray[number] which is shit since you want to know if the item is included and it may be of any type. In bonus, if type is included in the array, the type will be true */
function includes(array, item) {
return array?.includes(item);
}
exports.includes = includes;
//# sourceMappingURL=array-utils.js.map