1// 22.1.3.3 Array.prototype.copyWithin(target, start, end = this.length)
2'use strict';
3var toObject = require('./_to-object');
4var toAbsoluteIndex = require('./_to-absolute-index');
5var toLength = require('./_to-length');
6
7module.exports = [].copyWithin || function copyWithin(target /* = 0 */, start /* = 0, end = @length */) {
8  var O = toObject(this);
9  var len = toLength(O.length);
10  var to = toAbsoluteIndex(target, len);
11  var from = toAbsoluteIndex(start, len);
12  var end = arguments.length > 2 ? arguments[2] : undefined;
13  var count = Math.min((end === undefined ? len : toAbsoluteIndex(end, len)) - from, len - to);
14  var 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};
27