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

Refactor validateNumber(...) #67

Open
kungfooman opened this issue Dec 2, 2023 · 0 comments
Open

Refactor validateNumber(...) #67

kungfooman opened this issue Dec 2, 2023 · 0 comments
Assignees
Labels
type assertions Everything related to type assertions. typings Everything related to type declarations

Comments

@kungfooman
Copy link
Owner

Currently it has some issues with false positives:

["str", "1", "NaN", NaN, new Date].map(isNaN);
// Outputs: [true, false, true, true, false]

It tests for: undefined / null / NaN / infinity... but it seems we also need a simple typeof check:

validateNumber(["1", 2, 3], 0); // Approved, but it should fail

function validateNumber(obj, prop) {
const val = obj[prop];
const type = typeof obj;
if (val === null) {
typecheckWarn(`${type}#${prop} null`, {obj});
return false;
}
if (val === undefined) {
typecheckWarn(`${type}#${prop} undefined`, {obj});
return false;
}
if (isNaN(val)) {
typecheckWarn(`${type}#${prop} NaN`, {obj});
return false;
}
if (!isFinite(val)) {
typecheckWarn(`${type}#${prop} +-Infinity`, {obj});
return false;
}
return true;
}

And then I also have to fix validateNumber @todo note in validateType.

@kungfooman kungfooman self-assigned this Dec 2, 2023
@kungfooman kungfooman added type assertions Everything related to type assertions. typings Everything related to type declarations labels Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type assertions Everything related to type assertions. typings Everything related to type declarations
Projects
None yet
Development

No branches or pull requests

1 participant