Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var isArguments = require('../lang/isArguments'),
2
isArray = require('../lang/isArray'),
3
isArrayLike = require('./isArrayLike'),
4
isObjectLike = require('./isObjectLike');
5
6
/**
7
* The base implementation of `_.flatten` with added support for restricting
8
* flattening and specifying the start index.
9
*
10
* @private
11
* @param {Array} array The array to flatten.
12
* @param {boolean} [isDeep] Specify a deep flatten.
13
* @param {boolean} [isStrict] Restrict flattening to arrays-like objects.
14
* @returns {Array} Returns the new flattened array.
15
*/
16
function baseFlatten(array, isDeep, isStrict) {
17
var index = -1,
18
length = array.length,
19
resIndex = -1,
20
result = [];
21
22
while (++index < length) {
23
var value = array[index];
24
if (isObjectLike(value) && isArrayLike(value) &&
25
(isStrict || isArray(value) || isArguments(value))) {
26
if (isDeep) {
27
// Recursively flatten arrays (susceptible to call stack limits).
28
value = baseFlatten(value, isDeep, isStrict);
29
}
30
var valIndex = -1,
31
valLength = value.length;
32
33
while (++valIndex < valLength) {
34
result[++resIndex] = value[valIndex];
35
}
36
} else if (!isStrict) {
37
result[++resIndex] = value;
38
}
39
}
40
return result;
41
}
42
43
module.exports = baseFlatten;
44
45