@newdash/newdash
Version:
javascript/typescript utility library
42 lines (41 loc) • 1.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const isFlattenable_1 = __importDefault(require("./isFlattenable"));
const arrayPush_1 = __importDefault(require("./arrayPush"));
/**
* The base implementation of `flatten` with support for restricting flattening.
*
* @private
* @ignore
* @param {Array} array The array to flatten.
* @param {number} depth The maximum recursion depth.
* @param {(value:any)=>boolean} [predicate=isFlattenable] The function invoked per iteration.
* @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
* @param {Array} [result=[]] The initial result value.
* @returns {Array} Returns the new flattened array.
*/
function baseFlatten(array, depth, predicate, isStrict, result) {
var index = -1, length = array.length;
predicate || (predicate = isFlattenable_1.default);
result || (result = []);
while (++index < length) {
var value = array[index];
if (depth > 0 && predicate(value)) {
if (depth > 1) {
// Recursively flatten arrays (susceptible to call stack limits).
baseFlatten(value, depth - 1, predicate, isStrict, result);
}
else {
(0, arrayPush_1.default)(result, value);
}
}
else if (!isStrict) {
result[result.length] = value;
}
}
return result;
}
exports.default = baseFlatten;