unicorn/prefer-array-some
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-array-some": "error",
},
},
];Prefer .some(…) over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…).
const hasUnicorn = array.filter(element => isUnicorn(element)).length > 0;const hasUnicorn = array.filter(element => isUnicorn(element)).length !== 0;const hasUnicorn = array.find(element => isUnicorn(element)) !== undefined;const hasUnicorn = array.some(element => isUnicorn(element));if (array.some(element => isUnicorn(element))) {
// …
}const found = array.some(item => item > 2);
Boolean(found);Examples from the ESLint documentation.
unicorn/prefer-array-some
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-array-some": "error",
},
},
];unicorn/prefer-array-some
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-array-some": "error"
}
}complexity/useArraySome
complexity · since 2.4.5
{
"linter": {
"rules": {
"complexity": {
"useArraySome": "error"
}
}
}
}Enforce the style of numeric separators by correctly grouping digits.
Prefer .at() method for index access and String#charAt().
Disallow if statements as the only statement in if blocks without else.
Prefer JavaScript modules (ESM) over CommonJS.
Prefer RegExp#test() over String#match(), String#search(), and RegExp#exec().
Prefer String#slice() over String#substr() and String#substring().