eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: No multiple array check in logical expression
Description
Use .some() or .every() check on one array repeatly can simplify to use one call.
Fail
if (
array.some(element => element.foo === 1)
|| array.some(element => element.bar === 2)
) {}
if (
array.every(element => element.foo === 1)
&& array.every(element => element.bar === 2)
) {}
Pass
if (
array.some(element => element.foo === 1 || element.bar === 2)
) {}
if (
array.every(element => element.foo === 1 && element.bar === 2)
) {}
Proposed rule name
Not sure about the name yet
Additional Info
No response
Inside boolean context, I think it could als handle .find and its variants exactly the same as it would .some.
Rule name: no-multiple-array-checks-in-logical-expressions?
Inside boolean context, I think it could als handle
.findand its variants exactly the same as it would.some.
Did you mean
if (
array.find(element => element.foo === 1)
&& array.find(element => element.bar === 2)
) {}
?
prefer-array-some should already handled?
Accepted