Skip to content

Latest commit

Β 

History

History
61 lines (44 loc) Β· 1.61 KB

File metadata and controls

61 lines (44 loc) Β· 1.61 KB

no-useless-length-check

πŸ“ 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() returns false for an empty array. There is no need to check if the array is not empty.
  • Array#every() returns true for 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.

Examples

// ❌
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);