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

au revoir TypedArray toSpliced #98

Merged
merged 2 commits into from Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -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:
Expand Down
11 changes: 0 additions & 11 deletions polyfill.d.ts
Expand Up @@ -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 {};
16 changes: 1 addition & 15 deletions polyfill.js
Expand Up @@ -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);
Expand Down Expand Up @@ -315,7 +301,7 @@
});
}

/** @type {(def: { [N in "with" | "toReversed" | "toSorted" | "toSpliced"]?: (this: TypedArray, ...args: Parameters<Uint8Array[N]>) => TypedArray }) => void} */
/** @type {(def: { [N in "with" | "toReversed" | "toSorted"]?: (this: TypedArray, ...args: Parameters<Uint8Array[N]>) => TypedArray }) => void} */
function defineTypedArrayMethods(def) {
defineMethods(typedArrayPrototype, def);
}
Expand Down
88 changes: 0 additions & 88 deletions polyfill.test.js
Expand Up @@ -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]);
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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();
});
Expand Down
1 change: 0 additions & 1 deletion polyfill.test262.mjs
Expand Up @@ -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}`
Expand Down
50 changes: 0 additions & 50 deletions spec.html
Expand Up @@ -411,56 +411,6 @@ <h1>%TypedArray%.prototype.toSorted ( _comparefn_ )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-%typedarray%.prototype.toSpliced">
<h1>%TypedArray%.prototype.toSpliced ( _start_, _deleteCount_, ..._items_ )</h1>

<p>When the *toSpliced* method is called, the following steps are taken:</p>

<emu-alg>
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 -&infin;, let _actualStart_ be 0.
1. Else if _relativeStart_ &lt; 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_, &laquo; 𝔽(_newLen_) &raquo;).
1. Let _i_ be 0.
1. Let _r_ be _actualStart_ + _actualDeleteCount_.
1. Repeat, while _i_ &lt; _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_ &lt; _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_.
</emu-alg>
</emu-clause>

<emu-clause id="sec-%typedarray%.prototype.with">
<h1>%TypedArray%.prototype.with ( _index_, _value_ )</h1>

Expand Down
2 changes: 1 addition & 1 deletion test262
Submodule test262 updated 2472 files