mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
import isArguments from '../lang/isArguments';
|
|
import isArray from '../lang/isArray';
|
|
import isArrayLike from './isArrayLike';
|
|
import isObjectLike from './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-like objects.
|
|
* @returns {Array} Returns the new flattened array.
|
|
*/
|
|
function baseFlatten(array, isDeep, isStrict) {
|
|
var index = -1,
|
|
length = array.length,
|
|
resIndex = -1,
|
|
result = [];
|
|
|
|
while (++index < length) {
|
|
var value = array[index];
|
|
if (isObjectLike(value) && isArrayLike(value) &&
|
|
(isStrict || 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;
|
|
|
|
while (++valIndex < valLength) {
|
|
result[++resIndex] = value[valIndex];
|
|
}
|
|
} else if (!isStrict) {
|
|
result[++resIndex] = value;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export default baseFlatten;
|