index.js 587 B

123456789101112131415161718192021222324252627282930313233
  1. /*!
  2. * array-slice <https://github.com/jonschlinkert/array-slice>
  3. *
  4. * Copyright (c) 2014-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. module.exports = function slice(arr, start, end) {
  9. var len = arr.length;
  10. var range = [];
  11. start = idx(len, start);
  12. end = idx(len, end, len);
  13. while (start < end) {
  14. range.push(arr[start++]);
  15. }
  16. return range;
  17. };
  18. function idx(len, pos, end) {
  19. if (pos == null) {
  20. pos = end || 0;
  21. } else if (pos < 0) {
  22. pos = Math.max(len + pos, 0);
  23. } else {
  24. pos = Math.min(pos, len);
  25. }
  26. return pos;
  27. }