UNPKG

969 BJavaScriptView Raw
1'use strict';
2var toObject = require('../internals/to-object');
3var toAbsoluteIndex = require('../internals/to-absolute-index');
4var toLength = require('../internals/to-length');
5
6var min = Math.min;
7
8// `Array.prototype.copyWithin` method implementation
9// https://tc39.github.io/ecma262/#sec-array.prototype.copywithin
10module.exports = [].copyWithin || function copyWithin(target /* = 0 */, start /* = 0, end = @length */) {
11 var O = toObject(this);
12 var len = toLength(O.length);
13 var to = toAbsoluteIndex(target, len);
14 var from = toAbsoluteIndex(start, len);
15 var end = arguments.length > 2 ? arguments[2] : undefined;
16 var count = min((end === undefined ? len : toAbsoluteIndex(end, len)) - from, len - to);
17 var inc = 1;
18 if (from < to && to < from + count) {
19 inc = -1;
20 from += count - 1;
21 to += count - 1;
22 }
23 while (count-- > 0) {
24 if (from in O) O[to] = O[from];
25 else delete O[to];
26 to += inc;
27 from += inc;
28 } return O;
29};