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

use 'to' prefix for method names #50

Merged
merged 1 commit into from
Dec 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ This proposal is currently at [Stage 2].

This proposal introduces the following function properties to `Array.prototype`:

- `Array.prototype.withReversed() -> Array`
- `Array.prototype.withSorted(compareFn) -> Array`
- `Array.prototype.withSpliced(start, deleteCount, ...items) -> Array`
- `Array.prototype.withAt(index, value) -> Array`
- `Array.prototype.toReversed() -> Array`
- `Array.prototype.toSorted(compareFn) -> Array`
- `Array.prototype.toSpliced(start, deleteCount, ...items) -> Array`
- `Array.prototype.with(index, value) -> Array`

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:

- `TypedArray.prototype.withReversed() -> TypedArray`
- `TypedArray.prototype.withSorted(compareFn) -> TypedArray`
- `TypedArray.prototype.withSpliced(start, deleteCount, ...items) -> TypedArray`
- `TypedArray.prototype.withAt(index, value) -> TypedArray`
- `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 avaliable on subclasses of `TypedArray`. i.e. the following:

Expand All @@ -61,15 +61,15 @@ These methods will then be avaliable on subclasses of `TypedArray`. i.e. the fol

```js
const sequence = [1, 2, 3];
sequence.withReversed(); // => [3, 2, 1]
sequence.toReversed(); // => [3, 2, 1]
sequence; // => [1, 2, 3]

const outOfOrder = new Uint8Array([3, 1, 2]);
outOfOrder.withSorted(); // => Uint8Array [1, 2, 3]
outOfOrder.toSorted(); // => Uint8Array [1, 2, 3]
outOfOrder; // => Uint8Array [3, 1, 2]

const correctionNeeded = [1, 1, 3];
correctionNeeded.withAt(1, 2); // => [1, 2, 3]
correctionNeeded.with(1, 2); // => [1, 2, 3]
correctionNeeded; // => [1, 1, 3]
```

Expand Down
80 changes: 40 additions & 40 deletions polyfill.d.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
declare global {
interface Array<T> {
withAt(index: number, value: T): T[];
withReversed(): T[];
withSorted(compareFn?: (a: T, b: T) => number): T[];
withSpliced(start: number, deleteCount?: number, ...values: T[]): T[];
with(index: number, value: T): T[];
toReversed(): T[];
toSorted(compareFn?: (a: T, b: T) => number): T[];
toSpliced(start: number, deleteCount?: number, ...values: T[]): T[];
}

interface Int8Array {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
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 {
withAt(index: number, value: number): this;
withReversed(): this;
withSorted(compareFn?: (a: number, b: number) => number): this;
withSpliced(start: number, deleteCount?: number, ...values: number[]): this;
with(index: number, value: number): this;
toReversed(): this;
toSorted(compareFn?: (a: number, b: number) => number): this;
toSpliced(start: number, deleteCount?: number, ...values: number[]): this;
}
}
export {};
39 changes: 21 additions & 18 deletions polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
}
}

/** @type {Array["withReversed"]} */
/** @type {Array["toReversed"]} */
function arrayReversed() {
const o = Object(this);
const len = lengthOfArrayLike(o);
Expand All @@ -105,7 +105,7 @@
return a;
}

/** @type {TypedArray["withReversed"]} */
/** @type {TypedArray["toReversed"]} */
function typedArrayReversed() {
const o = assertTypedArray(this);
const len = o.length;
Expand All @@ -114,7 +114,7 @@
return a;
}

/** @type {Array["withSorted"]} */
/** @type {Array["toSorted"]} */
function arraySorted(compareFn) {
if (compareFn !== void 0 && typeof compareFn !== "function") {
throw new TypeError();
Expand All @@ -127,7 +127,7 @@
return a;
}

/** @type {TypedArray["withSorted"]} */
/** @type {TypedArray["toSorted"]} */
function typedArraySorted(compareFn) {
if (compareFn !== void 0 && typeof compareFn !== "function") {
throw new TypeError();
Expand Down Expand Up @@ -185,7 +185,7 @@
}
}

/** @type {Array["withSpliced"]} */
/** @type {Array["toSpliced"]} */
function arraySpliced(start, deleteCount, ...values) {
const o = Object(this);
const len = lengthOfArrayLike(o);
Expand All @@ -195,7 +195,7 @@
return a;
}

/** @type {TypedArray["withSpliced"]} */
/** @type {TypedArray["toSpliced"]} */
function typedArraySpliced(start, deleteCount, ...values) {
const o = assertTypedArray(this);
const len = o.length;
Expand All @@ -205,8 +205,8 @@
return a;
}

/** @type {Array["withAt"]} */
function arrayWithAt(index, value) {
/** @type {Array["with"]} */
function arrayWith(index, value) {
const o = Object(this);
const len = lengthOfArrayLike(o);
const actualIndex = index < 0 ? len + index : index;
Expand All @@ -221,8 +221,8 @@
return a;
}

/** @type {TypedArray["withAt"]} */
function typedArrayWithAt(index, value) {
/** @type {TypedArray["with"]} */
function typedArrayWith(index, value) {
const o = assertTypedArray(this);
const len = o.length;
const actualIndex = index < 0 ? len + index : index;
Expand Down Expand Up @@ -258,22 +258,25 @@
}

const arrayMethods = {
withAt: arrayWithAt,
withReversed: arrayReversed,
withSorted: arraySorted,
withSpliced: arraySpliced,
with: arrayWith,
toReversed: arrayReversed,
toSorted: arraySorted,
toSpliced: arraySpliced,
};

addToPrototype(arrayPrototype, arrayMethods);

for (const method of Object.keys(arrayMethods)) {
if (method === 'with') {
continue; // 'with' is already a keyword
}
arrayPrototype[Symbol.unscopables][method] = true;
}

addToPrototype(typedArrayPrototype, {
withAt: typedArrayWithAt,
withReversed: typedArrayReversed,
withSorted: typedArraySorted,
withSpliced: typedArraySpliced,
with: typedArrayWith,
toReversed: typedArrayReversed,
toSorted: typedArraySorted,
toSpliced: typedArraySpliced,
});
})(Array.prototype, Object.getPrototypeOf(Int8Array.prototype));
Loading