-
Notifications
You must be signed in to change notification settings - Fork 92
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
isValid method throws error if undefined is passed in #33
Comments
Duplicate of #32 |
That PR is closed as not merged, so the fact that the |
The precondition for |
That's the thing though, you can't be sure it's a string, and if the code is specifically tailored to only work with certain kinds of input (in this case a string or array), then it should always default to the fail case, which in this case would be |
All it would take to fix it and make it bulletproof is check the type is a string: if typeof string !== "string" || string.indexOf(":") === -1 Otherwise you're intentionally leaving a hole in your code that will generate unhandled exceptions for people if malformed data is passed in. Since there's no callback, there's no way to properly handle it from my code's perspective should unexpected data go in, even more so when it's part of another module |
that doesn't handle String objects though. |
|
=> new String("x").indexOf |
oh well, nevermind, that wouldn't break the check, just make it slightly slower. |
Awesome! Sorry to be a bit pedantic about this, but technically that check still isn't right as you should be checking that string is not a string then return false. That was my mistake in my original comment which I edited later but you probably missed it, apologies! It passes your test now because it never does the 2nd check of |
That was done deliberately. You can see that the logical operator is also reversed; this is to handle the case of passing a String object. |
Fair enough |
Thanks for fixing this 👍 |
No check on if string is defined (or is actually a string) is done before invoking
indexOf
which results in throwing an unhandled error instead of returningfalse
as would be expectedThe text was updated successfully, but these errors were encountered: