UNPKG

utilite

Version:

Powerful utility library for JS

69 lines (68 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.intersection = exports.union = exports.difference = exports.copyWithin = exports.getUniqueElements = exports.removeValuesFrom = exports.getTailN = exports.getHeadN = exports.findFirstMatching = exports.filterNegate = exports.filter = exports.filterFalsy = exports.fillArray = exports.isAnyMatchingLazy = exports.isAnyMatching = void 0; function isAnyMatching(array = [], mapper) { return array .map(mapper) .filter(Boolean) .reduce((a, b) => a ?? b, false); } exports.isAnyMatching = isAnyMatching; function isAnyMatchingLazy(array, predicate) { return array.some(predicate); } exports.isAnyMatchingLazy = isAnyMatchingLazy; function fillArray(length, value) { return Array(length).fill(value); } exports.fillArray = fillArray; function filterFalsy(array) { return array.filter((item) => item); } exports.filterFalsy = filterFalsy; function filter(array, predicate) { return array.filter(predicate); } exports.filter = filter; function filterNegate(array, predicate) { return filter(array, (item) => !predicate(item)); } exports.filterNegate = filterNegate; function findFirstMatching(array, predicate, defaultValue) { return array.find(predicate) ?? defaultValue; } exports.findFirstMatching = findFirstMatching; function getHeadN(array, limit = 0) { return array.slice(0, limit); } exports.getHeadN = getHeadN; function getTailN(array, limit = 0) { return limit <= 0 ? [] : array.slice(-limit); } exports.getTailN = getTailN; function removeValuesFrom(array, values) { return filter(array, (item) => !values.includes(item)); } exports.removeValuesFrom = removeValuesFrom; function getUniqueElements(array) { return Array.from(new Set(array)); } exports.getUniqueElements = getUniqueElements; function copyWithin(array, target, start, end = array.length) { const copied = array.slice(start, end); array.splice(target, copied.length, ...copied); return array; } exports.copyWithin = copyWithin; function difference(a1, a2) { return filter(a1, (item) => !a2.includes(item)); } exports.difference = difference; function union(a1, a2) { return Array.from(new Set([...a1, ...a2])); } exports.union = union; function intersection(a1, a2) { return filter(a1, (item) => a2.includes(item)); } exports.intersection = intersection;