-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathbaseFlatten.js
43 lines (38 loc) · 1.41 KB
/
baseFlatten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
define(['../lang/isArguments', '../lang/isArray', './isLength', './isObjectLike'], function(isArguments, isArray, isLength, isObjectLike) {
/**
* The base implementation of `_.flatten` with added support for restricting
* flattening and specifying the start index.
*
* @private
* @param {Array} array The array to flatten.
* @param {boolean} [isDeep] Specify a deep flatten.
* @param {boolean} [isStrict] Restrict flattening to arrays and `arguments` objects.
* @param {number} [fromIndex=0] The index to start from.
* @returns {Array} Returns the new flattened array.
*/
function baseFlatten(array, isDeep, isStrict, fromIndex) {
var index = (fromIndex || 0) - 1,
length = array.length,
resIndex = -1,
result = [];
while (++index < length) {
var value = array[index];
if (isObjectLike(value) && isLength(value.length) && (isArray(value) || isArguments(value))) {
if (isDeep) {
// Recursively flatten arrays (susceptible to call stack limits).
value = baseFlatten(value, isDeep, isStrict);
}
var valIndex = -1,
valLength = value.length;
result.length += valLength;
while (++valIndex < valLength) {
result[++resIndex] = value[valIndex];
}
} else if (!isStrict) {
result[++resIndex] = value;
}
}
return result;
}
return baseFlatten;
});