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 check for lenght === 0 in any object with lenght property #690

Closed
wants to merge 1 commit into from

Conversation

marioizquierdo
Copy link

Apply some duck typing ruby-style checks, and now isEmpty behavior is more intuitive.

Now you can do things like this (that is not true with the current implementation):

matched_elements = jQuery(selector);
if (!_.isEmpty(matched_elements)) {
  do_stuff();
}

In general, makes sense to think that anything with length === 0 is empty.

… you can do things like _.isEmpty(jQuery(selector));
@michaelficarra
Copy link
Collaborator

If the length property is enumerable, it's not empty. From the comment just a few lines up,

An "empty" object has no enumerable own-properties.

This function deals with objects and their properties, not arrays or array-like objects and their members.

@jashkenas jashkenas closed this Jul 31, 2012
@marioizquierdo
Copy link
Author

Just for curiosity, it would be nice to have a broken test, or at least an example here as where checking for length === 0 is not OK.
Thanks

@jdalton
Copy link
Contributor

jdalton commented Aug 1, 2012

@michaelficarra

This function deals with objects and their properties, not arrays or array-like objects and their members.

See line 830. The isEmpty method deals with arrays and strings, so practically array-like values :P

@marioizquierdo
Copy link
Author

@michaelficarra, @jashkenas

I guess the point here is this:

Underscore current version:

_.isEmpty({length: 0}); //=> false
_.isEmpty({length: null}); //=> false
_.isEmpty({foo: bar, length: 0}); //=> false

Pull request 690 (after this pull request):

_.isEmpty({length: 0}); //=> TRUE
_.isEmpty({length: null}); //=> false
_.isEmpty({foo: bar, length: 0}); //=> TRUE

This is not covered in the test suite.
If we clearly decide something, I am happy to add a test case for this, to avoid future "flaws" and document better the expected behavior of the isEmpty method.

@braddunbar
Copy link
Collaborator

FWIW, this patch would break the usage of isEmpty in Backbone.

_.isEmpty({}); // -> true
_.isEmpty({length: 0}); // -> false

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

Successfully merging this pull request may close these issues.

5 participants