UNPKG

super-utils-plus

Version:

A superior alternative to Lodash with improved performance, TypeScript support, and developer experience

132 lines (131 loc) 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.map = map; exports.flatMap = flatMap; exports.flatMapDeep = flatMapDeep; exports.flatMapDepth = flatMapDepth; const is_1 = require("../utils/is"); const flatten_1 = require("./flatten"); /** * Creates an array of values by running each element in collection through iteratee. * * @param collection - The collection to iterate over * @param iteratee - The function invoked per iteration * @returns The new mapped array * * @example * ```ts * function square(n) { * return n * n; * } * * map([4, 8], square); * // => [16, 64] * * map({ 'a': 4, 'b': 8 }, square); * // => [16, 64] (iteration order is not guaranteed) * * const users = [ * { 'user': 'barney' }, * { 'user': 'fred' } * ]; * * // The `_.property` iteratee shorthand. * map(users, 'user'); * // => ['barney', 'fred'] * ``` */ function map(collection, iteratee) { if (!collection || !collection.length) { return []; } // Convert iteratee to a function if it's a string let iterateeFn; if (typeof iteratee === 'string') { const key = iteratee; iterateeFn = (value) => { return (0, is_1.isObject)(value) ? value[key] : undefined; }; } else { iterateeFn = iteratee; } return collection.map(iterateeFn); } /** * Creates a flattened array of values by running each element in collection * through iteratee and flattening the mapped results. The iteratee is invoked * with three arguments: (value, index|key, collection). * * @param collection - The collection to iterate over * @param iteratee - The function invoked per iteration * @returns The new flattened array * * @example * ```ts * function duplicate(n) { * return [n, n]; * } * * flatMap([1, 2], duplicate); * // => [1, 1, 2, 2] * ``` */ function flatMap(collection, iteratee) { return (0, flatten_1.flatten)(map(collection, iteratee)); } /** * This method is like flatMap except that it recursively flattens the * mapped results. * * @param collection - The collection to iterate over * @param iteratee - The function invoked per iteration * @returns The new flattened array * * @example * ```ts * function duplicate(n) { * return [[[n, n]]]; * } * * flatMapDeep([1, 2], duplicate); * // => [1, 1, 2, 2] * ``` */ function flatMapDeep(collection, iteratee) { return (0, flatten_1.flattenDeep)(map(collection, iteratee)); } /** * This method is like flatMap except that it recursively flattens the * mapped results up to depth times. * * @param collection - The collection to iterate over * @param iteratee - The function invoked per iteration * @param depth - The maximum recursion depth * @returns The new flattened array * * @example * ```ts * function duplicate(n) { * return [[[n, n]]]; * } * * flatMapDepth([1, 2], duplicate, 2); * // => [[1, 1], [2, 2]] * ``` */ function flatMapDepth(collection, iteratee, depth = 1) { const mapped = map(collection, iteratee); // Custom flatten with depth const flattenWithDepth = (arr, currentDepth) => { return currentDepth > 0 ? arr.reduce((acc, item) => { if ((0, is_1.isArray)(item)) { return acc.concat(flattenWithDepth(item, currentDepth - 1)); } return acc.concat(item); }, []) : arr; }; return flattenWithDepth(mapped, depth); }