Change Array by copy
Provides additional methods to Array.prototype to enable changes on an array by returning a new copy of it with the change.
Status
This proposal is currently at Stage 1.
Champions
- Robin Ricard (Bloomberg)
- Ashley Claymore (Bloomberg)
Overview
This proposal introduces the following function properties to Array.prototype:
Array.prototype.withFilled(value, start, end) -> ArrayArray.prototype.withCopiedWithin(copiedTarget, start, end) -> ArrayArray.prototype.withPopped() -> ArrayArray.prototype.withPushed(values...) -> ArrayArray.prototype.withReversed() -> ArrayArray.prototype.withShifted() -> ArrayArray.prototype.withSorted(compareFn) -> ArrayArray.prototype.withSpliced(start, deleteCount, ...items) -> ArrayArray.prototype.withUnshifted(...values) -> ArrayArray.prototype.withAt(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.withFilled(value, start, end) -> TypedArrayTypedArray.prototype.withCopiedWithin(copiedTarget, start, end) -> TypedArrayTypedArray.prototype.withPopped() -> TypedArrayTypedArray.prototype.withPushed(values...) -> TypedArrayTypedArray.prototype.withReversed() -> TypedArrayTypedArray.prototype.withShifted() -> TypedArrayTypedArray.prototype.withSorted(compareFn) -> TypedArrayTypedArray.prototype.withSpliced(start, deleteCount, ...items) -> TypedArrayTypedArray.prototype.withUnshifted(...values) -> TypedArrayTypedArray.prototype.withAt(index, value) -> TypedArray
These methods will then be avaliable on subclasses of TypedArray. i.e. the following:
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64Array
Example
const sequence = [1, 2, 3];
sequence.withReversed(); // => [3, 2, 1]
sequence; // => [1, 2, 3]
const outOfOrder = new Uint8Array([3, 1, 2]);
outOfOrder.withSorted(); // => Uint8Array [1, 2, 3]
outOfOrder; // => Uint8Array [3, 1, 2]
const correctionNeeded = [1, 1, 3];
correctionNeeded.withAt(1, 2); // => [1, 2, 3]
correctionNeeded; // => [1, 1, 3]Motivation
The Tuple.prototype introduces those functions as a way to deal with the immutable aspect of the Tuples in Record & Tuple. While Arrays are not immutable by nature, this style of programming can be beneficial to users dealing with frozen arrays for instance.
This proposal notably makes it easier to write code able to deal with Arrays and Tuples interchangeably.
Relationship with Record & Tuple
While this proposal is derived from Record & Tuple, it should progress independently.
If web compatibility prescribes it, property names defined in this proposal are going to be changed. Those changes should be reflected on Tuple.prototype.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
