-
Notifications
You must be signed in to change notification settings - Fork 20
To change: Reduction of scope #27
Comments
How do you propose to replace |
Here is the example from mdn with spliced: let months = ['Jan', 'March', 'April', 'June'];
months = months.spliced(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months = months.spliced(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"] Would become: let months = ['Jan', 'March', 'April', 'June'];
months = [...months.slice(0, 1), "Feb", ...months.slice(1)];
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months = [...months.slice(0, 4), "May", ...months.slice(5)];
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"] One thing to note: this form is the most "dumb" application of splice with this model, you can be actually more brief in a case by case basis. However, I am open to reintroduce |
|
That is a fair point, I proposed this as I know splice is not everyone favorite's method but as it does not go against the rationale presented, there is a good case for keeping it. Same would be said of fill and copyWithin, will put them as questionned. |
comparisons with their equivalents: - arr.filled(value, start, end)
+ [...arr.slice(0, start), ...arr.slice(start, end).map(() => value), arr.slice(end)]
- arr.copiedWithin(target, start, end)
+ nonMutatingCopyWithin(arr, target, start, end); // implementation below
- arr.popped()
+ arr.slice(0, -1)
- arr.pushed(...items)
+ arr.concat([...items])
- arr.shifted()
+ arr.slice(1)
- arr.spliced(start, deleteCount, ...items)
+ [...arr.slice(0, start), ...items, ...arr.slice(start + deleteCount)]
- arr.unshifted(...items)
+ [...items, ...arr]
+ or arr.spliced(0, 0, ...items)
// extracted out as I couldn't find a one-liner that handled all paramerters
function nonMutatingCopyWithin(arr, relTarget, relStart = 0, relEnd = arr.length) {
const length = arr.length;
const target = relTarget >= 0 ? relTarget : length + relTarget;
const start = relStart >= 0 ? relStart: length + relStart;
const end = Math.min(relEnd >= 0 ? relEnd : length + relEnd, start + (length - target));
return [
...arr.slice(0, target),
...arr.slice(start, end),
...arr.slice(target + (end - start))
];
} please shout if there are mistakes, or cleaner alternatives |
@rricard unlike |
Agreed, maybe they are actually more valuable on TypedArrays however so if that is the case we would keep them |
Another aspect to consider for methods that have syntactic equivalents, is that the syntax would be type dependent.
So while a non-mutating version of |
Implementors have expressed support in a reduction of scope to reduce the maintenance cost of adding 20 new methods (10 x 2, Array and TypedArray). A reduced set of methods would look like:
Rationale behind dropped methods:
|
This reduction of scope is scheduled to be done on both this proposal & the Tuple prototype in Record & Tuple. tc39/proposal-record-tuple#227 (comment) |
This issue is a consolidation of #24 by @benlesh (thanks for opening the discussion on this!)
This thread intends to propose the following reduction of scope. This has been discussed in the last Record & Tuple meeting but has not been validated, pending the definition fo the exact scope and further discussion.
updated to reflect our current intent to change (See #27 (comment) )
The rationale behind this proposed change is that those functions could be replaced with slice & concatenation techniques that would be as explicit. It would also prevent unwanted over-copying of arrays, notably by preventing chaining of those operations. Finally it reduces web-compatibility risk.
The main objection would be that those methods have been designed around Tuples in the first place and could be useful there. As we want to keep both proposals in sync we would have to remove them from Tuples as well.
The text was updated successfully, but these errors were encountered: