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, _.size, etc. fail for ES2015 Set objects #2627

Closed
jonathaneunice opened this issue Nov 14, 2016 · 4 comments
Closed

_.isEmpty, _.size, etc. fail for ES2015 Set objects #2627

jonathaneunice opened this issue Nov 14, 2016 · 4 comments

Comments

@jonathaneunice
Copy link

Underscore returns incorrect results for Set objects. E.g. believes they are empty when they are not. I assume other Underscore functions for iteration, inclusion testing, and so on will similarly fail.

~$ node
> u = require('underscore')
...
> s = new Set()
Set {}
> u.isEmpty(s)
true
> s.add(44)
Set { 44 }
> u.isEmpty(s)
true
> u.size(s)
0
> u.VERSION
'1.8.3'

Several open issues mention ES2015 Set and Map (e.g. this one); none seem the same as this bug. Reading source for v1.9.0, it seems to rely on defaultKeys (alias for Object.keys). Object.keys does not produce keys for Set objects, so this is likely to fail in the same ways it does now. Similarly _.size seems to rely on the the .length property, which Set does not have; it has an idiosyncratic .size property instead, which the source does not seem to query.

Set objects are new, and seem very immature. Still, when JS fails to do the right thing, I look to Underscore to make things right, or at least a little more logical. Hoping that a near release can help wrangle the new structures.

@akre54
Copy link
Collaborator

akre54 commented Nov 14, 2016

I agree. In the short term, we should have a logic branch for _.isMap and _.isSet and check for obj.count. Want to open a pull?

@jonathaneunice
Copy link
Author

Considering taking a crack at it. Slightly daunted by the need to redefine core functions (e.g. getLength, nativeKeys) to incorporate Set and Map, or to add alternative logic paths ("are we dealing with these newfangled types? Okay, well then...") to a wide variety of Underscore functions. It's a considerable surgery for a first-time contributor. Any guidance?

@captbaritone
Copy link
Collaborator

@jonathaneunice Two tips I would give:

  1. Trust the tests. We have very good test coverage. As long as all existing tests continue to pass, you can assume you haven't broken anything too meaningful.

  2. Feel free to open incomplete pull requests for early feedback. If you are unsure if you are on the right track, we can point you in the right direction.

@jgonggrijp
Copy link
Collaborator

Duplicate of #2147.

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

Successfully merging a pull request may close this issue.

4 participants