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

_.isEmpty Behavior on numbers #432

Closed
siyegen opened this issue Jan 13, 2012 · 14 comments
Closed

_.isEmpty Behavior on numbers #432

siyegen opened this issue Jan 13, 2012 · 14 comments
Labels

Comments

@siyegen
Copy link

siyegen commented Jan 13, 2012

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.

@siyegen
Copy link
Author

siyegen commented Jan 13, 2012

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)

@jashkenas
Copy link
Owner

Yep, _.isEmpty is only defined for objects an arrays. You shouldn't use it on strings or numbers.

@michaelficarra
Copy link
Collaborator

You shouldn't use it on numbers. Strings are okay:

79f65b4
https://github.com/documentcloud/underscore/blob/322a64135dc3c4a649f102e962054310c2b39c04/underscore.js#L808

@josser
Copy link

josser commented Feb 20, 2014

So, what alternatives we have to use on numbers?

@morficus
Copy link

morficus commented Mar 8, 2014

@jashkenas or @michaelficarra: any particular reason why _.isNumber is not called with in _.isEmpty? (same way _.isArray and _.isString are being used)

@michaelficarra
Copy link
Collaborator

Any reason why it should?

@morficus
Copy link

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 :-)

@jashkenas
Copy link
Owner

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.

@jdalton
Copy link
Contributor

jdalton commented Mar 24, 2014

I think this could be cleared up with _.isEmpty doc tweaks. What devs are looking for is _.exists.

@melnikov-s
Copy link
Contributor

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, '');
}

@josser
Copy link

josser commented Mar 24, 2014

@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

@morficus
Copy link

+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.

@timurridjanovic
Copy link

I ran into this today.. I was using it with _.omit

_.omit(someObject, _.isEmpty);

watcharapon pushed a commit to watcharapon/netforce that referenced this issue Mar 16, 2016
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.
@panialex
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants