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