UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2var arrayWith = require('../internals/array-with');
3var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
4var isBigIntArray = require('../internals/is-big-int-array');
5var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
6var toBigInt = require('../internals/to-big-int');
7
8var aTypedArray = ArrayBufferViewCore.aTypedArray;
9var getTypedArrayConstructor = ArrayBufferViewCore.getTypedArrayConstructor;
10var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
11
12var PROPER_ORDER = !!function () {
13 try {
14 // eslint-disable-next-line no-throw-literal, es/no-typed-arrays, es/no-array-prototype-with -- required for testing
15 new Int8Array(1)['with'](2, { valueOf: function () { throw 8; } });
16 } catch (error) {
17 // some early implementations, like WebKit, does not follow the final semantic
18 // https://github.com/tc39/proposal-change-array-by-copy/pull/86
19 return error === 8;
20 }
21}();
22
23// `%TypedArray%.prototype.with` method
24// https://tc39.es/ecma262/#sec-%typedarray%.prototype.with
25exportTypedArrayMethod('with', { 'with': function (index, value) {
26 var O = aTypedArray(this);
27 var relativeIndex = toIntegerOrInfinity(index);
28 var actualValue = isBigIntArray(O) ? toBigInt(value) : +value;
29 return arrayWith(O, getTypedArrayConstructor(O), relativeIndex, actualValue);
30} }['with'], !PROPER_ORDER);