slice.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var baseSlice = require('./_baseSlice'),
  2. isIterateeCall = require('./_isIterateeCall'),
  3. toInteger = require('./toInteger');
  4. /**
  5. * Creates a slice of `array` from `start` up to, but not including, `end`.
  6. *
  7. * **Note:** This method is used instead of
  8. * [`Array#slice`](https://mdn.io/Array/slice) to ensure dense arrays are
  9. * returned.
  10. *
  11. * @static
  12. * @memberOf _
  13. * @since 3.0.0
  14. * @category Array
  15. * @param {Array} array The array to slice.
  16. * @param {number} [start=0] The start position.
  17. * @param {number} [end=array.length] The end position.
  18. * @returns {Array} Returns the slice of `array`.
  19. */
  20. function slice(array, start, end) {
  21. var length = array == null ? 0 : array.length;
  22. if (!length) {
  23. return [];
  24. }
  25. if (end && typeof end != 'number' && isIterateeCall(array, start, end)) {
  26. start = 0;
  27. end = length;
  28. }
  29. else {
  30. start = start == null ? 0 : toInteger(start);
  31. end = end === undefined ? length : toInteger(end);
  32. }
  33. return baseSlice(array, start, end);
  34. }
  35. module.exports = slice;