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

Sema: check that nr is integer #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

wmertens
Copy link

@wmertens wmertens commented Sep 4, 2019

I mistakenly assumed that 0.5 rps would work…

Copy link
Collaborator

@OlliV OlliV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If nr would be set to null this would happen: null | 0 === 0

@@ -137,6 +137,11 @@ export class Sema {
capacity?: number;
} = {}
) {
if ((nr | 0) !== nr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((nr | 0) !== nr) {
if (nr && !Number.isInteger(nr)) {

Copy link
Author

@wmertens wmertens Sep 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, nr should be a positive non-zero integer, so how about if (!nr || nr < 1 || (nr | 0) !== nr)?

Number.isInteger doesn't exist in IE, but maybe that's not an issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, that seems good to me!

Oh no, I didn't thought about that. The polyfill is actually quite simple:

Number.isInteger = Number.isInteger || function(value) {
  return typeof value === 'number' && 
    isFinite(value) && 
    Math.floor(value) === value;
};

Of course we shouldn't actually polyfill it but maybe just do these checks in the if clause.

Number.isFinite() seems to have an implemented in all browsers.

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

Successfully merging this pull request may close these issues.

2 participants