Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: unnecessary filter then find #29

Open
golopot opened this issue Apr 10, 2019 · 2 comments
Open

New rule: unnecessary filter then find #29

golopot opened this issue Apr 10, 2019 · 2 comments

Comments

@golopot
Copy link

golopot commented Apr 10, 2019

// invalid:
arr
  .filter(x => p(x))
  .find(x => q(x))

// valid
arr
  .find(x => p(x) && q(x))
// Outputs are equivalent (I think). Also potentially short-circuits faster.  

The fix is not always safe. For example when p and q has side effects that depends on the order of execution, or when p or q throws errors.

@vegerot
Copy link

vegerot commented May 15, 2020

Maybe also doing

arr
  .filter(x=>p(x))[0]

would be something valid to be caught by this

@freaktechnik
Copy link
Owner

Maybe also doing

arr
  .filter(x=>p(x))[0]

would be something valid to be caught by this

This case is already available with unicorn/prefer-array-find. (That plugin also offers a prefer-flat-map rule)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants