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
_.isEmpty Behavior on numbers #432
Comments
The use case I ran into this was doing something like this _.each(cow, function(v, k) {
if (_.isEmpty(v)) {
// do something with k because of v being empty
}
}); So cow.a would be treated just like cow.b and cow.c. I can code around it, but thought it would be so much neater if I didn't have to (and if it didn't break the spirit or vision of underscore) |
Yep, |
You shouldn't use it on numbers. Strings are okay: 79f65b4 |
So, what alternatives we have to use on numbers? |
@jashkenas or @michaelficarra: any particular reason why |
Any reason why it should? |
Well, yes. I'm glad you asked :-) They way I have seen most folks use Even the official documentation states "Returns true if object contains no values" - and a number should qualify as a value. If there I some type of larger implication in making this change that my I just can't see, then my 2nd-best suggestion would be to please update the documentation to clearly state that it should not be used for numbers :-) |
Why would you ever want to pass a number into
Quite right. In JavaScript, a number is (very sadly) not an object. |
I think this could be cleared up with |
It makes sense for an example implementation of _.isEmpty = function(a) {
return _.isEqual(a, {}) || _.isEqual(a, []) || _.isEqual(a, '');
} |
@jashkenas That's probably because in some other languages "empty" determines whether variable is empty or not :) http://www.php.net/manual/en/function.empty.php |
+1 to @josser - mostly because I have a PHP background :-p |
I ran into this today.. I was using it with _.omit
|
2. if value is empty or zero should be required* ======================== note: I found some issue about _.isEmpty function but not be fix yet.(jashkenas/underscore#432) ex: _.isEmpty(0) # true , normally it should return false but it's always return true if type of value is number.
the real use of isEmpty should be if the value is empty, the value that you don't know what it is, or wherever value that you need to know what it is, but js its a pile of bugs so I understand why it just generates more bugs |
isEmpty works fine on objects containing numbers as the only 'non-empty' values
However the individual k,v of a:1 is considered empty.
I did read through the issues and noted that originally _.isEmpty wasn't intended for strings, so this might be intended. If it's not intended then I have a branch I can make a pull request for.
The text was updated successfully, but these errors were encountered: