π Disallow useless array length check.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Array#some()returnsfalsefor an empty array. There is no need to check if the array is not empty.Array#every()returnstruefor an empty array. There is no need to check if the array is empty.
We only check .length === 0, .length !== 0, and .length > 0. These zero and non-zero length check styles are allowed in the unicorn/explicit-length-check rule. It is recommended to use them together.
// β
if (array.length === 0 || array.every(Boolean));// β
if (array.length !== 0 && array.some(Boolean));
// β
if (array.every(Boolean));// β
if (array.length > 0 && array.some(Boolean));
// β
if (array.some(Boolean));// β
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);
// β
const isAllTrulyOrEmpty = array.every(Boolean);// β
if (array.length === 0 || anotherCheck() || array.every(Boolean));// β
const isNonEmptyAllTrulyArray = array.length > 0 && array.every(Boolean);// β
const isEmptyArrayOrAllTruly = array.length === 0 || array.some(Boolean);