1
0

shim.js 1001 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var toInteger = require('../../../number/to-integer')
  3. , toPosInt = require('../../../number/to-pos-integer')
  4. , isPlainArray = require('../../is-plain-array')
  5. , isArray = Array.isArray, slice = Array.prototype.slice
  6. , hasOwnProperty = Object.prototype.hasOwnProperty, max = Math.max;
  7. module.exports = function (start, end) {
  8. var length, result, i;
  9. if (!this || !isArray(this) || isPlainArray(this)) {
  10. return slice.apply(this, arguments);
  11. }
  12. length = toPosInt(this.length);
  13. start = toInteger(start);
  14. if (start < 0) start = max(length + start, 0);
  15. else if (start > length) start = length;
  16. if (end === undefined) {
  17. end = length;
  18. } else {
  19. end = toInteger(end);
  20. if (end < 0) end = max(length + end, 0);
  21. else if (end > length) end = length;
  22. }
  23. if (start > end) start = end;
  24. result = new this.constructor(end - start);
  25. i = 0;
  26. while (start !== end) {
  27. if (hasOwnProperty.call(this, start)) result[i] = this[start];
  28. ++i;
  29. ++start;
  30. }
  31. return result;
  32. };