eslint-plugin-array-func icon indicating copy to clipboard operation
eslint-plugin-array-func copied to clipboard

New rule: unnecessary filter then find

Open golopot opened this issue 5 years ago • 2 comments

// 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.

golopot avatar Apr 10 '19 06:04 golopot

Maybe also doing

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

would be something valid to be caught by this

vegerot avatar May 15 '20 19:05 vegerot

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)

freaktechnik avatar Jul 30 '20 23:07 freaktechnik