shim.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. , hasOwnProperty = Object.prototype.hasOwnProperty
  7. , max = Math.max, min = Math.min;
  8. module.exports = function (target, start/*, end*/) {
  9. var o = validValue(this), end = arguments[2], l = toPosInt(o.length)
  10. , to, from, fin, count, direction;
  11. target = toInteger(target);
  12. start = toInteger(start);
  13. end = (end === undefined) ? l : toInteger(end);
  14. to = target < 0 ? max(l + target, 0) : min(target, l);
  15. from = start < 0 ? max(l + start, 0) : min(start, l);
  16. fin = end < 0 ? max(l + end, 0) : min(end, l);
  17. count = min(fin - from, l - to);
  18. direction = 1;
  19. if ((from < to) && (to < (from + count))) {
  20. direction = -1;
  21. from += count - 1;
  22. to += count - 1;
  23. }
  24. while (count > 0) {
  25. if (hasOwnProperty.call(o, from)) o[to] = o[from];
  26. else delete o[from];
  27. from += direction;
  28. to += direction;
  29. count -= 1;
  30. }
  31. return o;
  32. };