unicorn/prefer-set-size
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-set-size": "error",
},
},
];function isUnique(array) {
return [...new Set(array)].length === array.length;
}const items = new Set([1, 2, 3, 4, 5]);
if ([...items].length > 3) {
// do something
}const uniqueCount = [...new Set(userIds)].length;function isUnique(array) {
return new Set(array).size === array.length;
}const items = new Set([1, 2, 3, 4, 5]);
if (items.size > 3) {
// do something
}const uniqueCount = new Set(userIds).size;Examples from the ESLint documentation.
unicorn/prefer-set-size
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-set-size": "error",
},
},
];unicorn/prefer-set-size
unicorn plugin · correctness
{
"rules": {
"unicorn/prefer-set-size": "error"
}
}nursery/useSetSize
nursery · since 2.6.5
{
"linter": {
"rules": {
"nursery": {
"useSetSize": "error"
}
}
}
}Disallow useless array length check.
Disallow useless fallback when spreading in object literals.
Disallow passing single-element arrays to Promise methods.
Prevent calling EventTarget#removeEventListener() with the result of an expression.