$.array-copy-within.js 846 B

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