var cow = {a: 1, b: {}, c: []};
_.isEmpty(cow); // returns false
isEmpty works fine on objects containing numbers as the only 'non-empty' values
_.isEmpty(cow.a); // returns true
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 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, _.isEmpty
is only defined for objects an arrays. You shouldn't use it on strings or numbers.
You shouldn't use it on numbers. Strings are okay:
https://github.com/documentcloud/underscore/commit/79f65b440ad5e14ff2c624a836a3eff33465be9f
https://github.com/documentcloud/underscore/blob/322a64135dc3c4a649f102e962054310c2b39c04/underscore.js#L808
So, what alternatives we have to use on numbers?
@jashkenas or @michaelficarra: any particular reason why _.isNumber
is not called with in _.isEmpty
? (same way _.isArray
and _.isString
are being used)
Any reason why it should?
Well, yes. I'm glad you asked :-)
They way I have seen most folks use _.isEmpty
is pretty much to check if a value (any value) has been assigned to a particular variable. I have seen many developers run in to issues because of this.
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 isEmpty
, instead of just checking if the number is null?
Even the official documentation states "Returns true if object contains no values"
Quite right. In JavaScript, a number is (very sadly) not an object.
I think this could be cleared up with _.isEmpty
doc tweaks. What devs are looking for is _.exists.
It makes sense for _.isEmpty(a)
to mean "is a
an empty object?", kind of like if the statement : if obj == {}
worked on values and not on references. Therefore values like numbers would return false and so would null and undefined.
an example implementation of _.isEmpty
might be:
_.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
But I think just clarifying it in the documentation could also be enough. I seriously see folks run into this every other week.
I ran into this today.. I was using it with _.omit
_.omit(someObject, _.isEmpty);
Most helpful comment
Well, yes. I'm glad you asked :-)
They way I have seen most folks use
_.isEmpty
is pretty much to check if a value (any value) has been assigned to a particular variable. I have seen many developers run in to issues because of this.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 :-)