Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 4c194d9

Browse files
authored
au revoir TypedArray toSpliced (#98)
* au revoir TypedArray toSpliced * update test262 submodule
1 parent 9181025 commit 4c194d9

File tree

7 files changed

+3
-168
lines changed

7 files changed

+3
-168
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ This proposal introduces the following function properties to `Array.prototype`:
3333

3434
All of those methods keep the target Array untouched and returns a copy of it with the change performed instead.
3535

36-
They will also be added to TypedArrays:
36+
`toReversed`, `toSorted`, and `with` will also be added to TypedArrays:
3737

3838
- `TypedArray.prototype.toReversed() -> TypedArray`
3939
- `TypedArray.prototype.toSorted(compareFn) -> TypedArray`
40-
- `TypedArray.prototype.toSpliced(start, deleteCount, ...items) -> TypedArray`
4140
- `TypedArray.prototype.with(index, value) -> TypedArray`
4241

4342
These methods will then be available on subclasses of `TypedArray`. i.e. the following:

polyfill.d.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,66 @@ declare global {
2020
with(index: number, value: number): this;
2121
toReversed(): this;
2222
toSorted(compareFn?: (a: number, b: number) => number): this;
23-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
2423
}
2524

2625
interface Uint8Array {
2726
with(index: number, value: number): this;
2827
toReversed(): this;
2928
toSorted(compareFn?: (a: number, b: number) => number): this;
30-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
3129
}
3230

3331
interface Uint8ClampedArray {
3432
with(index: number, value: number): this;
3533
toReversed(): this;
3634
toSorted(compareFn?: (a: number, b: number) => number): this;
37-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
3835
}
3936

4037
interface Int16Array {
4138
with(index: number, value: number): this;
4239
toReversed(): this;
4340
toSorted(compareFn?: (a: number, b: number) => number): this;
44-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
4541
}
4642

4743
interface Uint16Array {
4844
with(index: number, value: number): this;
4945
toReversed(): this;
5046
toSorted(compareFn?: (a: number, b: number) => number): this;
51-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
5247
}
5348

5449
interface Int32Array {
5550
with(index: number, value: number): this;
5651
toReversed(): this;
5752
toSorted(compareFn?: (a: number, b: number) => number): this;
58-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
5953
}
6054

6155
interface Uint32Array {
6256
with(index: number, value: number): this;
6357
toReversed(): this;
6458
toSorted(compareFn?: (a: number, b: number) => number): this;
65-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
6659
}
6760

6861
interface Float32Array {
6962
with(index: number, value: number): this;
7063
toReversed(): this;
7164
toSorted(compareFn?: (a: number, b: number) => number): this;
72-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
7365
}
7466

7567
interface Float64Array {
7668
with(index: number, value: number): this;
7769
toReversed(): this;
7870
toSorted(compareFn?: (a: number, b: number) => number): this;
79-
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
8071
}
8172

8273
interface BigInt64Array {
8374
with(index: number, value: bigint): this;
8475
toReversed(): this;
8576
toSorted(compareFn?: (a: bigint, b: bigint) => number | bigint): this;
86-
toSpliced(start: number, deleteCount?: number, ...values: bigint[]): this;
8777
}
8878

8979
interface BigUint64Array {
9080
with(index: number, value: bigint): this;
9181
toReversed(): this;
9282
toSorted(compareFn?: (a: bigint, b: bigint) => number | bigint): this;
93-
toSpliced(start: number, deleteCount?: number, ...values: bigint[]): this;
9483
}
9584
}
9685
export {};

polyfill.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,6 @@
255255
}
256256
});
257257

258-
defineTypedArrayMethods({
259-
toSpliced(start, deleteCount, ...values) {
260-
const o = assertTypedArray(this);
261-
const len = typedArrayLength(o);
262-
const { actualStart, actualDeleteCount, newLen } = calculateSplice({ start, deleteCount, len, values, argsCount: arguments.length });
263-
const convertedValues = values.map(v => {
264-
return typedArrayNumberConversion(o, v);
265-
})
266-
const a = typedArrayCreate(o, newLen);
267-
doSplice({ src: o, target: a, actualStart, actualDeleteCount, values: convertedValues, newLen });
268-
return a;
269-
}
270-
});
271-
272258
defineArrayMethods({
273259
with(index, value) {
274260
const o = toObject(this);
@@ -315,7 +301,7 @@
315301
});
316302
}
317303

318-
/** @type {(def: { [N in "with" | "toReversed" | "toSorted" | "toSpliced"]?: (this: TypedArray, ...args: Parameters<Uint8Array[N]>) => TypedArray }) => void} */
304+
/** @type {(def: { [N in "with" | "toReversed" | "toSorted"]?: (this: TypedArray, ...args: Parameters<Uint8Array[N]>) => TypedArray }) => void} */
319305
function defineTypedArrayMethods(def) {
320306
defineMethods(typedArrayPrototype, def);
321307
}

polyfill.test.js

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -204,49 +204,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => {
204204
t.end();
205205
});
206206

207-
tape(`${TypedArray.name}.prototype.toSpliced`, (t) => {
208-
const orig = new TypedArray([1, -1, 0, -1, 4]);
209-
const expected = new TypedArray([1, 2, 3, 4]);
210-
const idx = 1;
211-
const delNum = 3;
212-
const ins = [2, 3];
213-
214-
const copy = orig.toSpliced(idx, delNum, ...ins);
215-
216-
t.deepEqual(copy, expected);
217-
t.notEqual(orig, copy);
218-
t.notDeepEqual(orig, copy);
219-
t.end();
220-
});
221-
222-
tape(`${TypedArray.name}.prototype.toSpliced performs type conversion early`, (t) => {
223-
const orig = new TypedArray([1]);
224-
const valueUserCodeWillInsert = 4;
225-
const userCodeReturnValue = 5;
226-
const expected = new TypedArray([valueUserCodeWillInsert, userCodeReturnValue]);
227-
228-
let userCodeExecuted = false;
229-
/** @type any */
230-
const val = {
231-
valueOf() {
232-
userCodeExecuted = true;
233-
orig[0] = valueUserCodeWillInsert;
234-
return userCodeReturnValue;
235-
}
236-
};
237-
238-
const idx = 1;
239-
const delNum = 0;
240-
const ins = [val];
241-
242-
const copy = orig.toSpliced(idx, delNum, ...ins);
243-
244-
t.deepEqual(copy, expected);
245-
t.notEqual(orig, copy);
246-
t.notDeepEqual(orig, copy);
247-
t.end();
248-
});
249-
250207
tape(`${TypedArray.name}.prototype.with`, (t) => {
251208
const orig = new TypedArray([1, 1, 3]);
252209
const expected = new TypedArray([1, 2, 3]);
@@ -325,7 +282,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => {
325282
assertType(orig.with(0, 0));
326283
assertType(orig.toReversed());
327284
assertType(orig.toSorted());
328-
assertType(orig.toSpliced(0, 0));
329285

330286
t.end();
331287
});
@@ -374,49 +330,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => {
374330
t.end();
375331
});
376332

377-
tape(`${BigIntArray.name}.prototype.toSpliced`, (t) => {
378-
const orig = new BigIntArray([1n, -1n, 0n, -1n, 4n]);
379-
const expected = new BigIntArray([1n, 2n, 3n, 4n]);
380-
const idx = 1;
381-
const delNum = 3;
382-
const ins = [2n, 3n];
383-
384-
const copy = orig.toSpliced(idx, delNum, ...ins);
385-
386-
t.deepEqual(copy, expected);
387-
t.notEqual(orig, copy);
388-
t.notDeepEqual(orig, copy);
389-
t.end();
390-
});
391-
392-
tape(`${BigIntArray.name}.prototype.toSpliced performs type conversion early`, (t) => {
393-
const orig = new BigIntArray([1n]);
394-
const valueUserCodeWillInsert = 4n;
395-
const userCodeReturnValue = 5n;
396-
const expected = new BigIntArray([valueUserCodeWillInsert, userCodeReturnValue]);
397-
398-
let userCodeExecuted = false;
399-
/** @type any */
400-
const val = {
401-
valueOf() {
402-
userCodeExecuted = true;
403-
orig[0] = valueUserCodeWillInsert;
404-
return userCodeReturnValue;
405-
}
406-
};
407-
408-
const idx = 1;
409-
const delNum = 0;
410-
const ins = [val];
411-
412-
const copy = orig.toSpliced(idx, delNum, ...ins);
413-
414-
t.deepEqual(copy, expected);
415-
t.notEqual(orig, copy);
416-
t.notDeepEqual(orig, copy);
417-
t.end();
418-
});
419-
420333
tape(`${BigIntArray.name}.prototype.with`, (t) => {
421334
const orig = new BigIntArray([1n, 1n, 3n]);
422335
const expected = new BigIntArray([1n, 2n, 3n]);
@@ -508,7 +421,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => {
508421
assertType(orig.with(0, 0n));
509422
assertType(orig.toReversed());
510423
assertType(orig.toSorted());
511-
assertType(orig.toSpliced(0, 0));
512424

513425
t.end();
514426
});

polyfill.test262.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const testsPaths = [
2424
["Array", "Symbol.unscopables/change-array-by-copy.js"],
2525
["TypedArray", "toReversed"],
2626
["TypedArray", "toSorted"],
27-
["TypedArray", "toSpliced"],
2827
["TypedArray", "with"],
2928
].map(
3029
([constructor, method]) => `test/built-ins/${constructor}/prototype/${method}`

spec.html

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -411,56 +411,6 @@ <h1>%TypedArray%.prototype.toSorted ( _comparefn_ )</h1>
411411
</emu-alg>
412412
</emu-clause>
413413

414-
<emu-clause id="sec-%typedarray%.prototype.toSpliced">
415-
<h1>%TypedArray%.prototype.toSpliced ( _start_, _deleteCount_, ..._items_ )</h1>
416-
417-
<p>When the *toSpliced* method is called, the following steps are taken:</p>
418-
419-
<emu-alg>
420-
1. Let _O_ be the *this* value.
421-
1. Perform ? ValidateTypedArray(_O_).
422-
1. Let _len_ be _O_.[[ArrayLength]].
423-
1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_).
424-
1. If _relativeStart_ is -&infin;, let _actualStart_ be 0.
425-
1. Else if _relativeStart_ &lt; 0, let _actualStart_ be max(_len_ + _relativeStart_, 0).
426-
1. Else, let _actualStart_ be min(_relativeStart_, _len_).
427-
1. If _start_ is not present, then
428-
1. Let _actualDeleteCount_ be 0.
429-
1. Else if _deleteCount_ is not present, then
430-
1. Let _actualDeleteCount_ be _len_ - _actualStart_.
431-
1. Else,
432-
1. Let _dc_ be ? ToIntegerOrInfinity(_deleteCount_).
433-
1. Let _actualDeleteCount_ be the result of clamping _dc_ between 0 and _len_ - _actualStart_.
434-
1. Let _insertCount_ be the number of elements in _items_.
435-
1. Let _convertedItems_ be a new empty List.
436-
1. For each element _E_ of _items_, do
437-
1. If _O_.[[ContentType]] is ~BigInt~, let _convertedValue_ be ? ToBigInt(_E_).
438-
1. Else, let _convertedValue_ be ? ToNumber(_E_).
439-
1. Append _convertedValue_ as the last element of _convertedItems_.
440-
1. Let _newLen_ be _len_ + _insertCount_ - _actualDeleteCount_.
441-
1. Let _A_ be ? TypedArrayCreateSameType(_O_, &laquo; 𝔽(_newLen_) &raquo;).
442-
1. Let _i_ be 0.
443-
1. Let _r_ be _actualStart_ + _actualDeleteCount_.
444-
1. Repeat, while _i_ &lt; _actualStart_,
445-
1. Let _Pi_ be ! ToString(𝔽(_i_)).
446-
1. Let _iValue_ be ! Get(_O_, _Pi_).
447-
1. Perform ! Set(_target_, _Pi_, _iValue_, *true*).
448-
1. Set _i_ to _i_ + 1.
449-
1. For each element _E_ of _convertedItems_, do
450-
1. Let _Pi_ be ! ToString(𝔽(_i_)).
451-
1. Perform ! Set(_A_, _Pi_, _E_, *true*).
452-
1. Set _i_ to _i_ + 1.
453-
1. Repeat, while _i_ &lt; _newLen_,
454-
1. Let _Pi_ be ! ToString(𝔽(_i_)).
455-
1. Let _from_ be ! ToString(𝔽(_r_)).
456-
1. Let _fromValue_ be ! Get(_O_, _from_).
457-
1. Perform ! Set(_A_, _Pi_, _fromValue_, *true*).
458-
1. Set _i_ to _i_ + 1.
459-
1. Set _r_ to _r_ + 1.
460-
1. Return _A_.
461-
</emu-alg>
462-
</emu-clause>
463-
464414
<emu-clause id="sec-%typedarray%.prototype.with">
465415
<h1>%TypedArray%.prototype.with ( _index_, _value_ )</h1>
466416

test262

Submodule test262 updated 2472 files

0 commit comments

Comments
 (0)