shim.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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
  6. , slice = Array.prototype.slice
  7. , objHasOwnProperty = Object.prototype.hasOwnProperty
  8. , max = Math.max;
  9. module.exports = function (start, end) {
  10. var length, result, i;
  11. if (!this || !isArray(this) || isPlainArray(this)) {
  12. return slice.apply(this, arguments);
  13. }
  14. length = toPosInt(this.length);
  15. start = toInteger(start);
  16. if (start < 0) start = max(length + start, 0);
  17. else if (start > length) start = length;
  18. if (end === undefined) {
  19. end = length;
  20. } else {
  21. end = toInteger(end);
  22. if (end < 0) end = max(length + end, 0);
  23. else if (end > length) end = length;
  24. }
  25. if (start > end) start = end;
  26. result = new this.constructor(end - start);
  27. i = 0;
  28. while (start !== end) {
  29. if (objHasOwnProperty.call(this, start)) result[i] = this[start];
  30. ++i;
  31. ++start;
  32. }
  33. return result;
  34. };