/**
* 创建一个新数组,将array与任何数组或值连接在一起
* Creates a new array concatenating `array` with any additional arrays and/or values
*
* @param {Array} array The array to concatenate
* @param {...*} [values] The values to concatenate
* @returns {Array} Returns the new concatenated array
* @example
*
* var array = [1];
* var other = concat(array, 2, [3], [[4]]);
*
* console.log(other)
* // => [1, 2, 3, [4]]
* console.log(arrat)
* // => [1]
*/
//内置的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
}
// Copies the values of `source` to `array`
function copyArray(source, array) {
var index = -1,
length = source.length
array || (array = Array(length))
while (++index < length) {
array[index] = source[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 concat() {
var length = arguments.length
if (!length) {
return []
}
var args = Array(length - 1),
array = arguments[0],
index = length
while (index--) {
args[index - 1] = arguments[index]
}
return arrayPush(
Array.isArray(array) ? copyArray(array) : [array],
baseFlatten(args, 1)
)
}
export default concat