创建一个切片数组,去除array前面的n个元素(n默认值为1)

/**
 * 创建一个切片数组,去除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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值