question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `prefer-change-array-by-copy`

See original GitHub issue

“Change Array by copy” proposal

Fail

array.reverse();
array.sort();
const array = [...foo];
array.splice(0, 1);
const array = [...foo];
array[1] = 'changed';

Pass

array = array.withReversed();
array = array.withSorted();
const array = foo.withSpliced(0, 1);
const array = foo.withAt(1, 'changed');

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
fiskercommented, Dec 16, 2021

The proposal has updated, see https://github.com/tc39/proposal-change-array-by-copy/commit/752e576db3d55e9d1d80a97df1f1e54c26ffbd7f

  • withReversed -> toReversed
  • withSorted -> toSorted
  • withSpliced -> toSpliced
  • withAt -> with
1reaction
fregantecommented, Mar 31, 2022

One pattern that I’ve been using is to clone arrays in the function parameters and I hope it can be preserved. I think the same thing applies to array destructuring.

Before

function foo(originalBar) {
	const bar = [...originalBar];
	return bar;
}

After

function foo([...bar] /* Shallow bar clone*/) {
	return bar;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Names of functions · Issue #10 · tc39/proposal-change-array ...
This proposal notably makes it easier to write code able to deal with Arrays and Tuples interchangeably. This strikes me as a non-argument. ......
Read more >
Copy array of objects and make changes without modifying ...
I have an array of objects. I would like to deep copy the array of objects and make some changes to each object....
Read more >
Proposed rule: Private Fund Advisers - SEC.gov
We propose to require registered investment advisers to private funds to provide transparency to their investors regarding the full cost of ...
Read more >
ECMAScript proposal “Change Array by copy”: four new non ...
This blog post describes the ECMAScript proposal “Change Array by copy” by Robin Ricard and Ashley Claymore. It proposes four new methods ...
Read more >
Array.prototype.remove - Ideas - TC39 - Discourse
EDIT : As noted below, it would be logical to include Array.prototype.insert in the proposal as well. 1 Like. ljharb May 3, 2021,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found