1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var toObject = require('../internals/to-object');
|
---|
4 | var toAbsoluteIndex = require('../internals/to-absolute-index');
|
---|
5 | var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
|
---|
6 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
7 | var setArrayLength = require('../internals/array-set-length');
|
---|
8 | var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
|
---|
9 | var arraySpeciesCreate = require('../internals/array-species-create');
|
---|
10 | var createProperty = require('../internals/create-property');
|
---|
11 | var deletePropertyOrThrow = require('../internals/delete-property-or-throw');
|
---|
12 | var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
|
---|
13 |
|
---|
14 | var HAS_SPECIES_SUPPORT = arrayMethodHasSpeciesSupport('splice');
|
---|
15 |
|
---|
16 | var max = Math.max;
|
---|
17 | var min = Math.min;
|
---|
18 |
|
---|
19 | // `Array.prototype.splice` method
|
---|
20 | // https://tc39.es/ecma262/#sec-array.prototype.splice
|
---|
21 | // with adding support of @@species
|
---|
22 | $({ target: 'Array', proto: true, forced: !HAS_SPECIES_SUPPORT }, {
|
---|
23 | splice: function splice(start, deleteCount /* , ...items */) {
|
---|
24 | var O = toObject(this);
|
---|
25 | var len = lengthOfArrayLike(O);
|
---|
26 | var actualStart = toAbsoluteIndex(start, len);
|
---|
27 | var argumentsLength = arguments.length;
|
---|
28 | var insertCount, actualDeleteCount, A, k, from, to;
|
---|
29 | if (argumentsLength === 0) {
|
---|
30 | insertCount = actualDeleteCount = 0;
|
---|
31 | } else if (argumentsLength === 1) {
|
---|
32 | insertCount = 0;
|
---|
33 | actualDeleteCount = len - actualStart;
|
---|
34 | } else {
|
---|
35 | insertCount = argumentsLength - 2;
|
---|
36 | actualDeleteCount = min(max(toIntegerOrInfinity(deleteCount), 0), len - actualStart);
|
---|
37 | }
|
---|
38 | doesNotExceedSafeInteger(len + insertCount - actualDeleteCount);
|
---|
39 | A = arraySpeciesCreate(O, actualDeleteCount);
|
---|
40 | for (k = 0; k < actualDeleteCount; k++) {
|
---|
41 | from = actualStart + k;
|
---|
42 | if (from in O) createProperty(A, k, O[from]);
|
---|
43 | }
|
---|
44 | A.length = actualDeleteCount;
|
---|
45 | if (insertCount < actualDeleteCount) {
|
---|
46 | for (k = actualStart; k < len - actualDeleteCount; k++) {
|
---|
47 | from = k + actualDeleteCount;
|
---|
48 | to = k + insertCount;
|
---|
49 | if (from in O) O[to] = O[from];
|
---|
50 | else deletePropertyOrThrow(O, to);
|
---|
51 | }
|
---|
52 | for (k = len; k > len - actualDeleteCount + insertCount; k--) deletePropertyOrThrow(O, k - 1);
|
---|
53 | } else if (insertCount > actualDeleteCount) {
|
---|
54 | for (k = len - actualDeleteCount; k > actualStart; k--) {
|
---|
55 | from = k + actualDeleteCount - 1;
|
---|
56 | to = k + insertCount - 1;
|
---|
57 | if (from in O) O[to] = O[from];
|
---|
58 | else deletePropertyOrThrow(O, to);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | for (k = 0; k < insertCount; k++) {
|
---|
62 | O[k + actualStart] = arguments[k + 2];
|
---|
63 | }
|
---|
64 | setArrayLength(O, len - actualDeleteCount + insertCount);
|
---|
65 | return A;
|
---|
66 | }
|
---|
67 | });
|
---|