/**
* 创建一个切片数组,去除array前面的n个元素(n默认值为1)
* Creates a slice of `array` with `n` elements dropped from the beginning
* @param {Array} array The array to query
* @param {number} [n=1] The number of elements to drop
* @param {Object} [guard] Enables use as an iteratee for methods like `map`
* @returns {Array} Returns the slice of `array`
* @example
* drop([1, 2, 3])
* // => [2, 3]
* drop([1, 2, 3], 2)
* // => [3]
* drop([1, 2, 3],5)
* // => []
* drop([1, 2, 3], 0)
* // => [1, 2, 3]
*/
const NAN = 0 / 0
const INFINITY = 1 / 0
const symbolTag = "[object Symbol]"
const MAX_INTEGER = Number.MAX_VALUE || 1.7976931348623157e308
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 baseSlice(array, start, end) {
var index = -1,
length = array.length
if (start < 0) {
start = -start > length ? 0 : length + start
}
end = end > length ? length : end
if (end < 0) {
end += length
}
length = start > end ? 0 : (end - start) >>> 0
start >>>= 0
var result = Array(length)
while (++index < length) {
result[index] = array[index + start]
}
return result
}
function drop(array, n, guard) {
var length = array == null ? 0 : array.length
if (!length) {
return []
}
n = guard || n === undefined ? 1 : toInteger(n)
return baseSlice(array, n < 0 ? 0 : n, length)
}
export default drop
/**
* 创建一个切片数组,去除array尾部的n个元素(n的默认值为1)
* Creates a slice of `array` with `n` elements dropped from the end
* @param {Array} array The array to query
* @param {number} [n=1] The number of elements to drop
* @param {Object} [guard] Enables use as an iteratee for methods like `map`
* @returns {Array} Returns the slice of `array`
* @example
* dropRight([1, 2, 3])
* // => [1, 2]
* dropRight([1, 2, 3], 2)
* // => [1]
* dropRight([1, 2, 3], 5)
* // => []
* dropRight([1, 2, 3], 0)
* // => [1, 2, 3]
*/
import toInteger from "./toInteger"
import baseSlice from "./baseSlice"
function dropRight(array, n, guard) {
var length = array == null ? 0 : array.length
if (!length) {
return []
}
n = guard || n === undefined ? 1 : toInteger(n)
n = length - n
return baseSlice(array, 0, n < 0 ? 0 : n)
}
export default dropRight