/**
* 减少一级array嵌套深度
* Flattens `array` a single level deep
* @param {Array} array The array to flatten
* @returns {Array} Returns the new flattened array
* @example
* flatten([1, [2, [3, [4]], 5]])
* // => [1, 2, [3, [4]], 5]
*/
//内置的Symbol.isConcatSpreadable符号用于配置某对象作为Array.prototype.concat()方法的参数时是否展开其数组元素
const spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined
// Appends the elements of `value` to `array`
function arrayPush(array, values) {
var index = -1,
length = values.length,
offset = array.length
while (++index < length) {
array[offset + index] = values[index]
}
return array
}
function isObjectLike(value) {
return typeof value == "object" && value !== null;
}
// Checks if `value` is likely an `arguments` object
function baseIsArguments(value) {
return isObjectLike(value) && Object.prototype.toString.call(value) === argsTag
}
const isArguments = baseIsArguments(
(function() {
return arguments
})()
)
? baseIsArguments
: function(value) {
return (
isObjectLike(value) &&
Object.prototype.hasOwnProperty.call(value, "callee") &&
!Object.prototype.propertyIsEnumerable.call(value, "callee")
)
}
// Checks if `value` is a flattenable `arguments` object or array
function isFlattenable(value) {
return (
Array.isArray(value) ||
isArguments(value) ||
!!(spreadableSymbol && value && value[spreadableSymbol])
)
}
// Flattens `array` a single level deep.
function baseFlatten(array, depth, predicate, isStrict, result) {
var index = -1,
length = array.length
predicate || (predicate = isFlattenable)
result || (result = [])
while (++index < length) {
var value = array[index]
if (depth > 0 && predicate(value)) {
if (depth > 1) {
baseFlatten(array, depth - 1, predicate, isStrict, result)
} else {
arrayPush(result, value)
}
} else if (!isStrict) {
result[result.length] = value
}
}
return result
}
function flatten(array) {
var length = array == null ? 0 : array.length
return length ? baseFlatten(array, 1) : []
}
export default flatten