-
Notifications
You must be signed in to change notification settings - Fork 117
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
_.isInteger([1]) returns true #252
Comments
@JavaScriptDude Thank you for reaching out. I agree the behavior is somewhat surprising. However, judging by the code comments and the use of underscore-contrib/underscore.function.predicates.js Lines 46 to 64 in 9eb0a98
This is because If this is not the behavior you want, you can define a new function that is closer to your liking. Perhaps like this: function guardedIsInteger(value) {
return (typeof value === 'number' || value instanceof Number) && _.isInteger(value);
} If we do a 2.0 version at some point, we might redesign the behavior. Until that time, though, we should be careful not to break intended behavior. Other users may be relying on it. |
I strongly suggest 2.0 update. FYI - reading the referenced stackoverflow post, the jQuery example works correctly as they check for array. Thanks for your consideration. |
@JavaScriptDude I must remind you that it is your opinion that There will likely be a 2.0 at some point, but I'm going to hold off on that until Underscore 2.0 comes along. The behavior of |
Random drop-in from ye olde maintainer. I agree that this behavior is surprising and should probably be fixed. Arguably it doesn't technically require a major version bump, since the documentation doesn't seem to indicate that arrays with one element are one of the types it intends to accept. Nevertheless, practically, I agree that waiting for 2.0 is best to avoid surprising breakage. |
One way to fix this without breaking would be to add a second parameter called |
I'll be glad to pass on a PR if you would like. I already have a similar functionality working in my internal libraries. |
It is perhaps a bit odd to have a |
There is an issue with the function predictates
isInteger()
andisFloat()
where a number is passed in an array of length 1.eg:
The fix for this should be pretty easy by just adding
if (i === undefined || i === null || i instanceof Array) return false
to the top of isNumeric.This simple gate will block undefined, null and array.
Suggestions?
The text was updated successfully, but these errors were encountered: