diff --git a/README.md b/README.md index 41e247d..cc77da2 100644 --- a/README.md +++ b/README.md @@ -33,11 +33,10 @@ This proposal introduces the following function properties to `Array.prototype`: All of those methods keep the target Array untouched and returns a copy of it with the change performed instead. -They will also be added to TypedArrays: +`toReversed`, `toSorted`, and `with` will also be added to TypedArrays: - `TypedArray.prototype.toReversed() -> TypedArray` - `TypedArray.prototype.toSorted(compareFn) -> TypedArray` -- `TypedArray.prototype.toSpliced(start, deleteCount, ...items) -> TypedArray` - `TypedArray.prototype.with(index, value) -> TypedArray` These methods will then be available on subclasses of `TypedArray`. i.e. the following: diff --git a/polyfill.d.ts b/polyfill.d.ts index 1c024b8..dd40bc1 100644 --- a/polyfill.d.ts +++ b/polyfill.d.ts @@ -20,77 +20,66 @@ declare global { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Uint8Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Uint8ClampedArray { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Int16Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Uint16Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Int32Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Uint32Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Float32Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface Float64Array { with(index: number, value: number): this; toReversed(): this; toSorted(compareFn?: (a: number, b: number) => number): this; - toSpliced(start: number, deleteCount?: number, ...values: number[]): this; } interface BigInt64Array { with(index: number, value: bigint): this; toReversed(): this; toSorted(compareFn?: (a: bigint, b: bigint) => number | bigint): this; - toSpliced(start: number, deleteCount?: number, ...values: bigint[]): this; } interface BigUint64Array { with(index: number, value: bigint): this; toReversed(): this; toSorted(compareFn?: (a: bigint, b: bigint) => number | bigint): this; - toSpliced(start: number, deleteCount?: number, ...values: bigint[]): this; } } export {}; diff --git a/polyfill.js b/polyfill.js index d5bf378..9812f66 100644 --- a/polyfill.js +++ b/polyfill.js @@ -255,20 +255,6 @@ } }); - defineTypedArrayMethods({ - toSpliced(start, deleteCount, ...values) { - const o = assertTypedArray(this); - const len = typedArrayLength(o); - const { actualStart, actualDeleteCount, newLen } = calculateSplice({ start, deleteCount, len, values, argsCount: arguments.length }); - const convertedValues = values.map(v => { - return typedArrayNumberConversion(o, v); - }) - const a = typedArrayCreate(o, newLen); - doSplice({ src: o, target: a, actualStart, actualDeleteCount, values: convertedValues, newLen }); - return a; - } - }); - defineArrayMethods({ with(index, value) { const o = toObject(this); @@ -315,7 +301,7 @@ }); } - /** @type {(def: { [N in "with" | "toReversed" | "toSorted" | "toSpliced"]?: (this: TypedArray, ...args: Parameters) => TypedArray }) => void} */ + /** @type {(def: { [N in "with" | "toReversed" | "toSorted"]?: (this: TypedArray, ...args: Parameters) => TypedArray }) => void} */ function defineTypedArrayMethods(def) { defineMethods(typedArrayPrototype, def); } diff --git a/polyfill.test.js b/polyfill.test.js index 5b3ab84..26b7f94 100644 --- a/polyfill.test.js +++ b/polyfill.test.js @@ -204,49 +204,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => { t.end(); }); - tape(`${TypedArray.name}.prototype.toSpliced`, (t) => { - const orig = new TypedArray([1, -1, 0, -1, 4]); - const expected = new TypedArray([1, 2, 3, 4]); - const idx = 1; - const delNum = 3; - const ins = [2, 3]; - - const copy = orig.toSpliced(idx, delNum, ...ins); - - t.deepEqual(copy, expected); - t.notEqual(orig, copy); - t.notDeepEqual(orig, copy); - t.end(); - }); - - tape(`${TypedArray.name}.prototype.toSpliced performs type conversion early`, (t) => { - const orig = new TypedArray([1]); - const valueUserCodeWillInsert = 4; - const userCodeReturnValue = 5; - const expected = new TypedArray([valueUserCodeWillInsert, userCodeReturnValue]); - - let userCodeExecuted = false; - /** @type any */ - const val = { - valueOf() { - userCodeExecuted = true; - orig[0] = valueUserCodeWillInsert; - return userCodeReturnValue; - } - }; - - const idx = 1; - const delNum = 0; - const ins = [val]; - - const copy = orig.toSpliced(idx, delNum, ...ins); - - t.deepEqual(copy, expected); - t.notEqual(orig, copy); - t.notDeepEqual(orig, copy); - t.end(); - }); - tape(`${TypedArray.name}.prototype.with`, (t) => { const orig = new TypedArray([1, 1, 3]); const expected = new TypedArray([1, 2, 3]); @@ -325,7 +282,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => { assertType(orig.with(0, 0)); assertType(orig.toReversed()); assertType(orig.toSorted()); - assertType(orig.toSpliced(0, 0)); t.end(); }); @@ -374,49 +330,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => { t.end(); }); - tape(`${BigIntArray.name}.prototype.toSpliced`, (t) => { - const orig = new BigIntArray([1n, -1n, 0n, -1n, 4n]); - const expected = new BigIntArray([1n, 2n, 3n, 4n]); - const idx = 1; - const delNum = 3; - const ins = [2n, 3n]; - - const copy = orig.toSpliced(idx, delNum, ...ins); - - t.deepEqual(copy, expected); - t.notEqual(orig, copy); - t.notDeepEqual(orig, copy); - t.end(); - }); - - tape(`${BigIntArray.name}.prototype.toSpliced performs type conversion early`, (t) => { - const orig = new BigIntArray([1n]); - const valueUserCodeWillInsert = 4n; - const userCodeReturnValue = 5n; - const expected = new BigIntArray([valueUserCodeWillInsert, userCodeReturnValue]); - - let userCodeExecuted = false; - /** @type any */ - const val = { - valueOf() { - userCodeExecuted = true; - orig[0] = valueUserCodeWillInsert; - return userCodeReturnValue; - } - }; - - const idx = 1; - const delNum = 0; - const ins = [val]; - - const copy = orig.toSpliced(idx, delNum, ...ins); - - t.deepEqual(copy, expected); - t.notEqual(orig, copy); - t.notDeepEqual(orig, copy); - t.end(); - }); - tape(`${BigIntArray.name}.prototype.with`, (t) => { const orig = new BigIntArray([1n, 1n, 3n]); const expected = new BigIntArray([1n, 2n, 3n]); @@ -508,7 +421,6 @@ tape("Array.prototype[Symbol.unscopables]", (t) => { assertType(orig.with(0, 0n)); assertType(orig.toReversed()); assertType(orig.toSorted()); - assertType(orig.toSpliced(0, 0)); t.end(); }); diff --git a/polyfill.test262.mjs b/polyfill.test262.mjs index 86c28c4..a259ec7 100644 --- a/polyfill.test262.mjs +++ b/polyfill.test262.mjs @@ -24,7 +24,6 @@ const testsPaths = [ ["Array", "Symbol.unscopables/change-array-by-copy.js"], ["TypedArray", "toReversed"], ["TypedArray", "toSorted"], - ["TypedArray", "toSpliced"], ["TypedArray", "with"], ].map( ([constructor, method]) => `test/built-ins/${constructor}/prototype/${method}` diff --git a/spec.html b/spec.html index 3f50dd2..7c236f6 100644 --- a/spec.html +++ b/spec.html @@ -411,56 +411,6 @@

%TypedArray%.prototype.toSorted ( _comparefn_ )

- -

%TypedArray%.prototype.toSpliced ( _start_, _deleteCount_, ..._items_ )

- -

When the *toSpliced* method is called, the following steps are taken:

- - - 1. Let _O_ be the *this* value. - 1. Perform ? ValidateTypedArray(_O_). - 1. Let _len_ be _O_.[[ArrayLength]]. - 1. Let _relativeStart_ be ? ToIntegerOrInfinity(_start_). - 1. If _relativeStart_ is -∞, let _actualStart_ be 0. - 1. Else if _relativeStart_ < 0, let _actualStart_ be max(_len_ + _relativeStart_, 0). - 1. Else, let _actualStart_ be min(_relativeStart_, _len_). - 1. If _start_ is not present, then - 1. Let _actualDeleteCount_ be 0. - 1. Else if _deleteCount_ is not present, then - 1. Let _actualDeleteCount_ be _len_ - _actualStart_. - 1. Else, - 1. Let _dc_ be ? ToIntegerOrInfinity(_deleteCount_). - 1. Let _actualDeleteCount_ be the result of clamping _dc_ between 0 and _len_ - _actualStart_. - 1. Let _insertCount_ be the number of elements in _items_. - 1. Let _convertedItems_ be a new empty List. - 1. For each element _E_ of _items_, do - 1. If _O_.[[ContentType]] is ~BigInt~, let _convertedValue_ be ? ToBigInt(_E_). - 1. Else, let _convertedValue_ be ? ToNumber(_E_). - 1. Append _convertedValue_ as the last element of _convertedItems_. - 1. Let _newLen_ be _len_ + _insertCount_ - _actualDeleteCount_. - 1. Let _A_ be ? TypedArrayCreateSameType(_O_, « 𝔽(_newLen_) »). - 1. Let _i_ be 0. - 1. Let _r_ be _actualStart_ + _actualDeleteCount_. - 1. Repeat, while _i_ < _actualStart_, - 1. Let _Pi_ be ! ToString(𝔽(_i_)). - 1. Let _iValue_ be ! Get(_O_, _Pi_). - 1. Perform ! Set(_target_, _Pi_, _iValue_, *true*). - 1. Set _i_ to _i_ + 1. - 1. For each element _E_ of _convertedItems_, do - 1. Let _Pi_ be ! ToString(𝔽(_i_)). - 1. Perform ! Set(_A_, _Pi_, _E_, *true*). - 1. Set _i_ to _i_ + 1. - 1. Repeat, while _i_ < _newLen_, - 1. Let _Pi_ be ! ToString(𝔽(_i_)). - 1. Let _from_ be ! ToString(𝔽(_r_)). - 1. Let _fromValue_ be ! Get(_O_, _from_). - 1. Perform ! Set(_A_, _Pi_, _fromValue_, *true*). - 1. Set _i_ to _i_ + 1. - 1. Set _r_ to _r_ + 1. - 1. Return _A_. - -
-

%TypedArray%.prototype.with ( _index_, _value_ )

diff --git a/test262 b/test262 index cc2bf39..5ac196d 160000 --- a/test262 +++ b/test262 @@ -1 +1 @@ -Subproject commit cc2bf398c985ada6c74c1eea26846465d2814eff +Subproject commit 5ac196d5619478a9bfe0905553d6ba103306d030