/**
* 使用value值来填充(替换)array, 从start位置开始,到end位置结束(但不包含end位置)。
* 这个方法会改变array(不是创建新数组)
* Fills elements of `array` with `value` from `start` up to, but not including `end`
* **Note:** This method mutates `array`
*
* @param {Array} array The array to fill
* @param {*} value The value to fill `array` with
* @param {number} [start=0] The start position
* @param {number} [end=array.length] The end position
* @returns {Array} Returns `array`
* @example
*
* var array = [1, 2, 3];
* fill(array, 'a')
* // => ['a', 'a', 'a']
* fill(Array(3), 2)
* // => [2, 2, 2]
* fill([4, 6, 8, 10], '*', 1, 3)
* // => [4, '*', '*', 10]
*/
const NAN = 0 / 0
const INFINITY = 1 / 0
const symbolTag = "[object Symbol]"
const MAX_INTEGER = Number.MAX_VALUE || 1.7976931348623157e308
const MAX_ARRAY_LENGTH = 4294967295
function isObjectLike(value) {
return typeof value == "object" && value !== null
}
function isSymbol(value) {
return (
typeof value === "symbol" ||
(isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
)
}
function toNumber(value) {
if (typeof value === "number") {
return value
}
if (isSymbol(value)) {
return NAN
}
return Number(value)
}
function toFinite(value) {
if (!value) {
return value === 0 ? value : 0
}
value = toNumber(value)
if (value === INFINITY || value === -INFINITY) {
var sign = value < 0 ? -1 : 1 //可用Math.sign代替
return sign * MAX_INTEGER
}
return value === value ? value : 0 //NaN 不等于 NaN
}
function toInteger(value) {
var result = toFinite(value)
var remainder = result % 1
return result === result ? (remainder ? result - remainder : result) : 0 //可用Math.trunc代替
}
function baseClamp(number, lower, upper) {
if (number === number) {
if (upper !== undefined) {
number = number < upper ? number : upper
}
if (lower !== undefined) {
number = number > lower ? number : lower
}
}
return number
}
function toLength(value) {
return value ? baseClamp(toInteger(value), 0, MAX_ARRAY_LENGTH) : 0
}
function baseFill(array, value, start, end) {
var length = array.length
start = toInteger(start)
if (start < 0) {
start = -start > length ? 0 : length + start
}
end = end === undefined || end > length ? length : toInteger(end)
if (end < 0) {
end += length
}
end = start > end ? 0 : toLength(end)
while (start < end) {
array[start++] = value
}
return array
}
function fill(array, value, start, end) {
var length = array == null ? 0 : array.length
if (!length) {
return []
}
if (start && typeof start !== "number") {
start = 0
end = length
}
return baseFill(array, value, start, end)
}
export default fill