Skip to content

Latest commit

 

History

History
179 lines (131 loc) · 5.54 KB

prefer-number-properties.md

File metadata and controls

179 lines (131 loc) · 5.54 KB

Prefer Number static properties over global ones

💼 This rule is enabled in the ✅ recommended config.

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

ECMAScript 2015 moved globals onto the Number constructor for consistency and to slightly improve them. This rule enforces their usage to limit the usage of globals:

Fail

const foo = parseInt('10', 2);
const foo = parseFloat('10.5');
const foo = isNaN(10);
const foo = isFinite(10);
if (Object.is(foo, NaN)) {}
const {parseInt} = Number;
const foo = parseInt('10', 2);

Pass

const foo = Number.parseInt('10', 2);
const foo = Number.parseFloat('10.5');
const foo = Number.isNaN(10);
const foo = Number.isFinite(10);
if (Object.is(foo, Number.NaN)) {}
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
const isPositiveZero = value => value === 0 && 1 / value === Infinity;
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;

Options

Type: object

checkInfinity

Type: boolean
Default: false

Pass checkInfinity: true to enable check on Infinity.

Fail

// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Infinity;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = -Infinity;

Pass

// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Number.POSITIVE_INFINITY;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const foo = Number.NEGATIVE_INFINITY;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
const foo = Infinity;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
const foo = -Infinity;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;

checkNaN

Type: boolean
Default: true

Pass checkNaN: false to disable check on NaN.

Fail

// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = NaN;
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = -NaN;

Pass

// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = Number.NaN;
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
const foo = -Number.NaN;
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
const foo = NaN;
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
const foo = -NaN;