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

Add support for maps in _.pairs method ...Closes #2517 #2523

Closed
wants to merge 3 commits into from

Conversation

penDerGraft
Copy link

_add support for maps within .pairs method by returning array of map entries.

I decided to go with a different approach. Returning an array of obj.entries() seemed a little bit cleaner than iterating over each entry, and seemed to follow the pattern of type-check -> return in the _.keys method. Happy to use a loop though if it is deemed the better option.

add support for maps within _.pairs method by returning array of map entries
@coveralls
Copy link

coveralls commented May 2, 2016

Coverage Status

Coverage increased (+0.004%) to 96.871% when pulling 675c8d2 on penDerGraft:master into 53086b0 on jashkenas:master.

@penDerGraft penDerGraft closed this May 2, 2016
@penDerGraft
Copy link
Author

Looking into failed tests...

@penDerGraft penDerGraft reopened this May 2, 2016
@penDerGraft penDerGraft closed this May 2, 2016
@coveralls
Copy link

coveralls commented May 2, 2016

Coverage Status

Coverage increased (+0.004%) to 96.871% when pulling 675c8d2 on penDerGraft:master into 53086b0 on jashkenas:master.

@akre54
Copy link
Collaborator

akre54 commented May 2, 2016

Should this support Sets too?

@penDerGraft
Copy link
Author

What would be the desired behavior for _.pairs of Sets? For Maps/Objects it is key, value, but Sets don't have quite the same structure. What should be returned?

@akre54
Copy link
Collaborator

akre54 commented May 2, 2016

I'm not entirely sure. It could be the same entries call, with the output
just passing that along (2 arrays of arrays, if I'm not mistaken)

@penDerGraft penDerGraft changed the title Closes #2517 Add support for maps in _.pairs method ...Closes #2517 May 2, 2016
@penDerGraft penDerGraft reopened this May 2, 2016
@penDerGraft penDerGraft closed this May 2, 2016
@penDerGraft penDerGraft reopened this May 2, 2016
@coveralls
Copy link

coveralls commented May 2, 2016

Coverage Status

Coverage increased (+0.03%) to 96.894% when pulling 742033f on penDerGraft:master into 53086b0 on jashkenas:master.

@coveralls
Copy link

coveralls commented May 2, 2016

Coverage Status

Coverage increased (+0.03%) to 96.894% when pulling 742033f on penDerGraft:master into 53086b0 on jashkenas:master.

@coveralls
Copy link

coveralls commented May 2, 2016

Coverage Status

Coverage increased (+0.03%) to 96.894% when pulling 742033f on penDerGraft:master into 53086b0 on jashkenas:master.

@penDerGraft
Copy link
Author

penDerGraft commented May 2, 2016

Using the logic in the last commit, if a check for _.isSet is added as well, it will return an array of arrays which contain two of each entry in the Set.

var s = new Set([1,2,3]);
_.pairs(s);
// => [[1,1], [2,2], [3,3]]

@akre54 If that's what you're envisioning, I can add the check.

@akre54
Copy link
Collaborator

akre54 commented May 2, 2016

Yeah I think it should be treated like an object with {[key]: key} (a la keymirror)

@penDerGraft
Copy link
Author

Ok, as part of this PR or a new one? Seems like it could be a separate issue.

pairs[i] = [keys[i], obj[keys[i]]];
var pairs, i, length;
if (_.isMap(obj)) {
var entries = obj.entries();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd you remove the Array.from here? Seems like if you had Map you'd also have Array.from.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not a fan of this much branching logic for Map (which I think you probably wouldn't even use Underscore with in the first place).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree that Array.from is a cleaner solution. Unfortunately it isn't supported in node 0.12.*, hence the failed tests.
node v0.12.13 below:

> typeof Map
'function'
> Array.from
undefined

Not sure on the question of whether or not to even use underscore with Maps. I saw #2517 and decided to submit a PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the first version it was supported in? We could also drop support
for node 0.12.x since it's pretty ancient by this point

On Mon, May 2, 2016, 18:44 Brent Pendergraft notifications@github.com
wrote:

In underscore.js
#2523 (comment):

@@ -1009,11 +1009,21 @@

// Convert an object into a list of [key, value] pairs.
_.pairs = function(obj) {

  • var keys = _.keys(obj);
  • var length = keys.length;
  • var pairs = Array(length);
  • for (var i = 0; i < length; i++) {
  •  pairs[i] = [keys[i], obj[keys[i]]];
    
  • var pairs, i, length;
  • if (_.isMap(obj)) {
  •  var entries = obj.entries();
    

Yeah, I agree that Array.from is a cleaner solution. Unfortunately it
isn't supported in node 0.12.*, hence the failed tests.

typeof Map'function'> Array.fromundefined

Not sure on the question of whether or not to even use underscore with
Maps. I saw #2517
#2517 (comment)
and decided to submit a PR.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/jashkenas/underscore/pull/2523/files/742033f3de979e46a99d3e9553088e5897d49952#r61817716

Adam K (mobile)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like 4.0.0, which I think was the next official node.js release. Everything between the two was io.js.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip. I had thought about that, but wasn't sure if it would cause confusion since Array#forEach is handled by _.each. So, _.toArray would handle Maps and _.pairs would use _.toArray?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See npm/toArray for an example of using a helper function like mapToArray.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just want to make sure I'm on the same page. So are we looking for a separate helper function here, or just the same _.pairs function being used in other places?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A helper function to implement both. I'm showing that the code cost is spread around because it can be used for more than instance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. Thank you.

@akre54
Copy link
Collaborator

akre54 commented May 2, 2016

Separate pull is fine, interested to hear the other maintainers thoughts on it too.

@coveralls
Copy link

coveralls commented May 3, 2016

Coverage Status

Coverage increased (+0.03%) to 96.894% when pulling 5355cc6 on penDerGraft:master into 53086b0 on jashkenas:master.

@@ -82,7 +82,10 @@
assert.deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can convert an object into pairs');
assert.deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', 2], ['length', 3]], '... even when one of them is "length"');
if (typeof Map === 'function') {
assert.deepEqual(_.pairs(new Map([['one', 1], ['two', 2]])), [['one', 1], ['two', 2]], '... or when object is a Map');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read in a previous PR that IE11 does not support initializing Maps with arrays in the constructor, hence the refactor here.

@jgonggrijp
Copy link
Collaborator

Closing for the same reason as #2808. See also #2147.

@jgonggrijp jgonggrijp closed this Apr 22, 2020
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.

None yet

5 participants