shim.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Taken from: https://github.com/paulmillr/es6-shim/
  2. "use strict";
  3. var toInteger = require("../../../number/to-integer")
  4. , toPosInt = require("../../../number/to-pos-integer")
  5. , validValue = require("../../../object/valid-value")
  6. , objHasOwnProperty = Object.prototype.hasOwnProperty
  7. , max = Math.max
  8. , min = Math.min;
  9. module.exports = function (target, start /*, end*/) {
  10. var arr = validValue(this)
  11. , end = arguments[2]
  12. , length = toPosInt(arr.length)
  13. , to
  14. , from
  15. , fin
  16. , count
  17. , direction;
  18. target = toInteger(target);
  19. start = toInteger(start);
  20. end = end === undefined ? length : toInteger(end);
  21. to = target < 0 ? max(length + target, 0) : min(target, length);
  22. from = start < 0 ? max(length + start, 0) : min(start, length);
  23. fin = end < 0 ? max(length + end, 0) : min(end, length);
  24. count = min(fin - from, length - to);
  25. direction = 1;
  26. if (from < to && to < from + count) {
  27. direction = -1;
  28. from += count - 1;
  29. to += count - 1;
  30. }
  31. while (count > 0) {
  32. if (objHasOwnProperty.call(arr, from)) arr[to] = arr[from];
  33. else delete arr[from];
  34. from += direction;
  35. to += direction;
  36. count -= 1;
  37. }
  38. return arr;
  39. };