Skip to content

Latest commit

 

History

History
114 lines (84 loc) · 3.36 KB

prefer-array-some.md

File metadata and controls

114 lines (84 loc) · 3.36 KB

Prefer .some(…) over .filter(…).length check and .{find,findLast}(…)

💼 This rule is enabled in the ✅ recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Prefer using Array#some over:

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.

This rule is fixable for .filter(…).length check and has a suggestion for .{find,findLast}(…).

Fail

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;
if (array.find(element => isUnicorn(element))) {
	// …
}
const foo = array.find(element => isUnicorn(element)) ? bar : baz;
const hasUnicorn = array.find(element => isUnicorn(element) !== undefined;
const hasUnicorn = array.find(element => isUnicorn(element) != null;
if (array.find(element => isUnicorn(element))) {
	// …
}
const foo = array.findLast(element => isUnicorn(element)) ? bar : baz;
const hasUnicorn = array.findLast(element => isUnicorn(element) !== undefined;
const hasUnicorn = array.findLast(element => isUnicorn(element) != null;
<template>
	<div v-if="array.find(element => isUnicorn(element))">Vue</div>
</template>
<template>
	<div v-if="array.findLast(element => isUnicorn(element))">Vue</div>
</template>
<template>
	<div v-if="array.filter(element => isUnicorn(element)).length > 0">Vue</div>
</template>

Pass

const hasUnicorn = array.some(element => isUnicorn(element));
if (array.some(element => isUnicorn(element))) {
	// …
}
const foo = array.find(element => isUnicorn(element)) || bar;
const foo = array.findLast(element => isUnicorn(element)) || bar;
<template>
	<div v-if="array.some(element => isUnicorn(element))">Vue</div>
</template>