dropRight.js 964 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var baseSlice = require('../internal/baseSlice'),
  2. isIterateeCall = require('../internal/isIterateeCall');
  3. /**
  4. * Creates a slice of `array` with `n` elements dropped from the end.
  5. *
  6. * @static
  7. * @memberOf _
  8. * @category Array
  9. * @param {Array} array The array to query.
  10. * @param {number} [n=1] The number of elements to drop.
  11. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  12. * @returns {Array} Returns the slice of `array`.
  13. * @example
  14. *
  15. * _.dropRight([1, 2, 3]);
  16. * // => [1, 2]
  17. *
  18. * _.dropRight([1, 2, 3], 2);
  19. * // => [1]
  20. *
  21. * _.dropRight([1, 2, 3], 5);
  22. * // => []
  23. *
  24. * _.dropRight([1, 2, 3], 0);
  25. * // => [1, 2, 3]
  26. */
  27. function dropRight(array, n, guard) {
  28. var length = array ? array.length : 0;
  29. if (!length) {
  30. return [];
  31. }
  32. if (guard ? isIterateeCall(array, n, guard) : n == null) {
  33. n = 1;
  34. }
  35. n = length - (+n || 0);
  36. return baseSlice(array, 0, n < 0 ? 0 : n);
  37. }
  38. module.exports = dropRight;