UNPKG

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