UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

75 lines (74 loc) 1.92 kB
/** * Removes all given indexes from the given array. * * @category Array * @category Package : @augment-vir/common * @example * * ```ts * import {filterOutIndexes} from '@augment-vir/common'; * * const result = filterOutIndexes( * [ * 'a', * 'b', * '', * ], * [ * 0, * 2, * ], * ); * // result is `['b']` * ``` * * @returns A new array (does not mutate). * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function filterOutIndexes(array, indexes) { return array.filter((_, index) => !indexes.includes(index)); } /** * Performs * [`[].map()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map) * and * [`[].filter()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) * (in that order) on an array with a single iteration. * * @category Array * @category Package : @augment-vir/common * @example * * ```ts * import {filterMap} from '@augment-vir/common'; * * const result = filterMap( * [ * 'a', * 'b', * '', * ], * // map callback * (value) => { * return `value-${value}`; * }, * // filter callback * (mappedValue, originalValue) => { * return !!originalValue; * }, * ); * // result is `['value-a', 'value-b']` * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function filterMap(inputArray, mapCallback, filterCallback) { return inputArray.reduce((accum, entry, index, originalArray) => { const mapOutput = mapCallback(entry, index, originalArray); const filterOutput = filterCallback(mapOutput, entry, index, originalArray); if (filterOutput) { accum.push(mapOutput); } return accum; }, []); }