@newdash/newdash
Version:
javascript/typescript utility library
37 lines (36 loc) • 1.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const baseFlatten_1 = __importDefault(require("./.internal/baseFlatten"));
/**
* Recursively flatten `array` up to `depth` times.
*
* @since 5.4.0
* @category Array
* @param array The array to flatten.
* @param depth The maximum recursion depth.
* @returns Returns the new flattened array.
* @see [[flatMap]],[[flatMapDeep]],[[flatMapDepth]],[[flattenDeep]]
* @example
*
* ```js
* 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) {
const length = array == null ? 0 : array.length;
if (!length) {
return [];
}
depth = depth === undefined ? 1 : +depth;
return (0, baseFlatten_1.default)(array, depth);
}
exports.default = flattenDepth;