Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: prefer-number-is-integer #1460

Open
jonahsnider opened this issue Aug 1, 2021 · 3 comments
Open

Rule proposal: prefer-number-is-integer #1460

jonahsnider opened this issue Aug 1, 2021 · 3 comments

Comments

@jonahsnider
Copy link
Contributor

jonahsnider commented Aug 1, 2021

Use Number.isInteger.
Not really sure if there's any benefit to using it. Maybe it's more performant. Makes the code marginally cleaner.

Fail

number % 1 === 0;

Pass

Number.isInteger(number);
@sindresorhus
Copy link
Owner

sindresorhus commented Aug 1, 2021

Makes the code marginally cleaner.

Marginally cleaner is an understatement. number % 1 === 0; is extremely unreadable and also fails if the passed in variable is not a number:

let number = [['1']];

// ...

number % 1 === 0;
//=> true

Some more cases it could catch: https://stackoverflow.com/a/34848042/64949

And it could also catch _.isInteger.

@sindresorhus sindresorhus changed the title Rule proposal: Use Number.isInteger Rule proposal: prefer-number-is-integer Aug 1, 2021
@sindresorhus
Copy link
Owner

This is now accepted.

@sindresorhus
Copy link
Owner

If anyone wants to work on this, see the initial attempt and feedback in #1509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants