π Prefer .some(β¦) over .filter(β¦).length check and .{find,findLast,findIndex,findLastIndex}(β¦).
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Prefer using Array#some over:
- Non-zero length check on the result of
Array#filter().
We only check .filter().length > 0 and .filter().length !== 0. These two non-zero length check styles are allowed in unicorn/explicit-length-check rule. It is recommended to use them together.
-
Using
Array#find()orArray#findLast()to ensure at least one element in the array passes a given check. -
Comparing the result of
Array#find()orArray#findLast()withundefined. -
Using
Array#findIndex()orArray#findLastIndex()to ensure at least one element in the array passes a given check.
This rule is fixable for .filter(β¦).length checks and .{findIndex,findLastIndex}(β¦).
This rule provides a suggestion for .{find,findLast}(β¦).
// β
const hasUnicorn = array.filter(element => isUnicorn(element)).length > 0;
// β
const hasUnicorn = array.filter(element => isUnicorn(element)).length !== 0;
// β
const hasUnicorn = array.filter(element => isUnicorn(element)).length >= 1;
// β
const hasUnicorn = array.find(element => isUnicorn(element)) !== undefined;
// β
const hasUnicorn = array.find(element => isUnicorn(element)) != null;
// β
const hasUnicorn = array.some(element => isUnicorn(element));// β
if (array.find(element => isUnicorn(element))) {
// β¦
}
// β
if (array.some(element => isUnicorn(element))) {
// β¦
}// β
const foo = array.find(element => isUnicorn(element)) ? bar : baz;
// β
const foo = array.find(element => isUnicorn(element)) || bar;// β
const hasUnicorn = array.findLast(element => isUnicorn(element)) !== undefined;
// β
const hasUnicorn = array.findLast(element => isUnicorn(element)) != null;
// β
const hasUnicorn = array.findIndex(element => isUnicorn(element)) !== -1;
// β
const hasUnicorn = array.findLastIndex(element => isUnicorn(element)) !== -1;// β
const foo = array.findLast(element => isUnicorn(element)) ? bar : baz;// β
const foo = array.findLast(element => isUnicorn(element)) || bar;<template>
<!-- β -->
<div v-if="array.find(element => isUnicorn(element))">Vue</div>
<!-- β -->
<div v-if="array.filter(element => isUnicorn(element)).length > 0">Vue</div>
<!-- β
-->
<div v-if="array.some(element => isUnicorn(element))">Vue</div>
</template><template>
<!-- β -->
<div v-if="array.findLast(element => isUnicorn(element))">Vue</div>
</template>