UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

41 lines (40 loc) 1.27 kB
const require_isArray = require("../predicate/isArray.js"); const require_isArrayLike = require("../predicate/isArrayLike.js"); const require_isArguments = require("../predicate/isArguments.js"); //#region src/compat/array/flattenDepth.ts /** * Recursively flattens array up to depth times. * * @template T * @param array - The array to flatten. * @param [depth=1] - The maximum recursion depth. * @returns Returns the new flattened array. * * @example * const array = [1, [2, [3, [4]], 5]]; * * flattenDepth(array, 1); * // => [1, 2, [3, [4]], 5] * * flattenDepth(array, 2); * // => [1, 2, 3, [4], 5] */ function flattenDepth(array, depth = 1) { if (!require_isArrayLike.isArrayLike(array)) return []; const result = []; const flooredDepth = Math.floor(depth); const recursive = (arr, currentDepth) => { for (let i = 0; i < arr.length; i++) { const item = arr[i]; if (isFlattenable(item) && currentDepth < flooredDepth) recursive(item, currentDepth + 1); else result.push(item); } }; recursive(Array.from(array), 0); return result; } function isFlattenable(value) { return require_isArray.isArray(value) || require_isArguments.isArguments(value) || Boolean(value && value[Symbol.isConcatSpreadable]); } //#endregion exports.flattenDepth = flattenDepth;