/
esnext.typed-array.to-spliced.js
68 lines (61 loc) · 2.59 KB
/
esnext.typed-array.to-spliced.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
// TODO: Remove from `core-js@4`
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var isBigIntArray = require('../internals/is-big-int-array');
var toAbsoluteIndex = require('../internals/to-absolute-index');
var toBigInt = require('../internals/to-big-int');
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
var fails = require('../internals/fails');
var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var max = Math.max;
var min = Math.min;
// some early implementations, like WebKit, does not follow the final semantic
var PROPER_ORDER = !fails(function () {
// eslint-disable-next-line es/no-typed-arrays -- required for testing
var array = new Int8Array([1]);
var spliced = array.toSpliced(1, 0, {
valueOf: function () {
array[0] = 2;
return 3;
}
});
return spliced[0] !== 2 || spliced[1] !== 3;
});
// `%TypedArray%.prototype.toSpliced` method
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSpliced
exportTypedArrayMethod('toSpliced', function toSpliced(start, deleteCount /* , ...items */) {
var O = aTypedArray(this);
var C = getTypedArrayConstructor(O);
var len = lengthOfArrayLike(O);
var actualStart = toAbsoluteIndex(start, len);
var argumentsLength = arguments.length;
var k = 0;
var insertCount, actualDeleteCount, thisIsBigIntArray, convertedItems, value, newLen, A;
if (argumentsLength === 0) {
insertCount = actualDeleteCount = 0;
} else if (argumentsLength === 1) {
insertCount = 0;
actualDeleteCount = len - actualStart;
} else {
actualDeleteCount = min(max(toIntegerOrInfinity(deleteCount), 0), len - actualStart);
insertCount = argumentsLength - 2;
if (insertCount) {
convertedItems = new C(insertCount);
thisIsBigIntArray = isBigIntArray(convertedItems);
for (var i = 2; i < argumentsLength; i++) {
value = arguments[i];
// FF30- typed arrays doesn't properly convert objects to typed array values
convertedItems[i - 2] = thisIsBigIntArray ? toBigInt(value) : +value;
}
}
}
newLen = len + insertCount - actualDeleteCount;
A = new C(newLen);
for (; k < actualStart; k++) A[k] = O[k];
for (; k < actualStart + insertCount; k++) A[k] = convertedItems[k - actualStart];
for (; k < newLen; k++) A[k] = O[k + actualDeleteCount - insertCount];
return A;
}, !PROPER_ORDER);