_array-copy-within.js 876 B

1234567891011121314151617181920212223242526
  1. // 22.1.3.3 Array.prototype.copyWithin(target, start, end = this.length)
  2. 'use strict';
  3. var toObject = require('./_to-object');
  4. var toAbsoluteIndex = require('./_to-absolute-index');
  5. var toLength = require('./_to-length');
  6. module.exports = [].copyWithin || function copyWithin(target /* = 0 */, start /* = 0, end = @length */) {
  7. var O = toObject(this);
  8. var len = toLength(O.length);
  9. var to = toAbsoluteIndex(target, len);
  10. var from = toAbsoluteIndex(start, len);
  11. var end = arguments.length > 2 ? arguments[2] : undefined;
  12. var count = Math.min((end === undefined ? len : toAbsoluteIndex(end, len)) - from, len - to);
  13. var inc = 1;
  14. if (from < to && to < from + count) {
  15. inc = -1;
  16. from += count - 1;
  17. to += count - 1;
  18. }
  19. while (count-- > 0) {
  20. if (from in O) O[to] = O[from];
  21. else delete O[to];
  22. to += inc;
  23. from += inc;
  24. } return O;
  25. };