Skip to content

Sema: check that nr is integer #35

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

throw new TypeError(
'nr (' + nr + ') should be an integer'
);
}
if (isFn(pauseFn) !== isFn(resumeFn)) {
throw new Error(
'pauseFn and resumeFn must be both set for pausing'
Expand Down