-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
es.typed-array.with.js
30 lines (27 loc) · 1.36 KB
/
es.typed-array.with.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
'use strict';
var arrayWith = require('../internals/array-with');
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var isBigIntArray = require('../internals/is-big-int-array');
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
var toBigInt = require('../internals/to-big-int');
var aTypedArray = ArrayBufferViewCore.aTypedArray;
var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
var PROPER_ORDER = !!function () {
try {
// eslint-disable-next-line no-throw-literal, es/no-typed-arrays, es/no-array-prototype-with -- required for testing
new Int8Array(1)['with'](2, { valueOf: function () { throw 8; } });
} catch (error) {
// some early implementations, like WebKit, does not follow the final semantic
// https://github.com/tc39/proposal-change-array-by-copy/pull/86
return error === 8;
}
}();
// `%TypedArray%.prototype.with` method
// https://tc39.es/ecma262/#sec-%typedarray%.prototype.with
exportTypedArrayMethod('with', { 'with': function (index, value) {
var O = aTypedArray(this);
var relativeIndex = toIntegerOrInfinity(index);
var actualValue = isBigIntArray(O) ? toBigInt(value) : +value;
return arrayWith(O, getTypedArrayConstructor(O), relativeIndex, actualValue);
} }['with'], !PROPER_ORDER);