/** * @file * @copyright 2020 Aleksej Komarov * @license MIT */ /** * Iterates over elements of collection, returning an array of all elements * iteratee returns truthy for. The predicate is invoked with three * arguments: (value, index|key, collection). * * If collection is 'null' or 'undefined', it will be returned "as is" * without emitting any errors (which can be useful in some cases). * * @returns {any[]} */ export const filter = (iterateeFn: (input: T, index: number, collection: T[]) => boolean) => (collection: T[]): T[] => { if (collection === null || collection === undefined) { return collection; } if (Array.isArray(collection)) { const result: T[] = []; for (let i = 0; i < collection.length; i++) { const item = collection[i]; if (iterateeFn(item, i, collection)) { result.push(item); } } return result; } throw new Error(`filter() can't iterate on type ${typeof collection}`); }; type MapFunction = { (iterateeFn: (value: T, index: number, collection: T[]) => U): (collection: T[]) => U[]; (iterateeFn: (value: T, index: K, collection: Record) => U): ( collection: Record ) => U[]; }; /** * Creates an array of values by running each element in collection * thru an iteratee function. The iteratee is invoked with three * arguments: (value, index|key, collection). * * If collection is 'null' or 'undefined', it will be returned "as is" * without emitting any errors (which can be useful in some cases). */ export const map: MapFunction = (iterateeFn) => (collection: T[]): U[] => { if (collection === null || collection === undefined) { return collection; } if (Array.isArray(collection)) { return collection.map(iterateeFn); } if (typeof collection === 'object') { return Object.entries(collection).map(([key, value]) => { return iterateeFn(value, key, collection); }); } throw new Error(`map() can't iterate on type ${typeof collection}`); }; const COMPARATOR = (objA, objB) => { const criteriaA = objA.criteria; const criteriaB = objB.criteria; const length = criteriaA.length; for (let i = 0; i < length; i++) { const a = criteriaA[i]; const b = criteriaB[i]; if (a < b) { return -1; } if (a > b) { return 1; } } return 0; }; /** * Creates an array of elements, sorted in ascending order by the results * of running each element in a collection thru each iteratee. * * Iteratees are called with one argument (value). * * @returns {any[]} */ export const sortBy = (...iterateeFns: ((input: T) => unknown)[]) => (array: T[]): T[] => { if (!Array.isArray(array)) { return array; } let length = array.length; // Iterate over the array to collect criteria to sort it by let mappedArray: { criteria: unknown[]; value: T; }[] = []; for (let i = 0; i < length; i++) { const value = array[i]; mappedArray.push({ criteria: iterateeFns.map((fn) => fn(value)), value, }); } // Sort criteria using the base comparator mappedArray.sort(COMPARATOR); // Unwrap values const values: T[] = []; while (length--) { values[length] = mappedArray[length].value; } return values; }; export const sort = sortBy(); /** * Returns a range of numbers from start to end, exclusively. * For example, range(0, 5) will return [0, 1, 2, 3, 4]. */ export const range = (start: number, end: number): number[] => new Array(end - start).fill(null).map((_, index) => index + start); /** * A fast implementation of reduce. */ export const reduce = (reducerFn, initialValue) => (array) => { const length = array.length; let i; let result; if (initialValue === undefined) { i = 1; result = array[0]; } else { i = 0; result = initialValue; } for (; i < length; i++) { result = reducerFn(result, array[i], i, array); } return result; }; /** * Creates a duplicate-free version of an array, using SameValueZero for * equality comparisons, in which only the first occurrence of each element * is kept. The order of result values is determined by the order they occur * in the array. * * It accepts iteratee which is invoked for each element in array to generate * the criterion by which uniqueness is computed. The order of result values * is determined by the order they occur in the array. The iteratee is * invoked with one argument: value. */ export const uniqBy = (iterateeFn?: (value: T) => unknown) => (array: T[]): T[] => { const { length } = array; const result: T[] = []; const seen: unknown[] = iterateeFn ? [] : result; let index = -1; outer: while (++index < length) { let value: T | 0 = array[index]; const computed = iterateeFn ? iterateeFn(value) : value; if (computed === computed) { let seenIndex = seen.length; while (seenIndex--) { if (seen[seenIndex] === computed) { continue outer; } } if (iterateeFn) { seen.push(computed); } result.push(value); } else if (!seen.includes(computed)) { if (seen !== result) { seen.push(computed); } result.push(value); } } return result; }; /* eslint-enable indent */ export const uniq = uniqBy(); type Zip = { [I in keyof T]: T[I] extends (infer U)[] ? U : never; }[]; /** * Creates an array of grouped elements, the first of which contains * the first elements of the given arrays, the second of which contains * the second elements of the given arrays, and so on. */ export const zip = (...arrays: T): Zip => { if (arrays.length === 0) { return []; } const numArrays = arrays.length; const numValues = arrays[0].length; const result: Zip = []; for (let valueIndex = 0; valueIndex < numValues; valueIndex++) { const entry: unknown[] = []; for (let arrayIndex = 0; arrayIndex < numArrays; arrayIndex++) { entry.push(arrays[arrayIndex][valueIndex]); } // I tried everything to remove this any, and have no idea how to do it. result.push(entry as any); } return result; }; /** * This method is like "zip" except that it accepts iteratee to * specify how grouped values should be combined. The iteratee is * invoked with the elements of each group. */ export const zipWith = (iterateeFn: (...values: T[]) => U) => (...arrays: T[][]): U[] => { return map((values: T[]) => iterateeFn(...values))(zip(...arrays)); };