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

Object-preserving map() function: mapValues #1953

Merged
merged 1 commit into from
Feb 6, 2015
Merged

Object-preserving map() function: mapValues #1953

merged 1 commit into from
Feb 6, 2015

Conversation

wilzbach
Copy link
Contributor

This adds the mapValues methods.

It works exactly like map, but preserves the object.

_.map2({a: 1, b: 2}, function(val, key) {
    return val * 2;
});
=> {a: 2, b: 4}

There are at least three issues about this function (#220 #1867 #1887), and as I also do this frequently it would be awesome to get this into underscore.

_.mapValues = function(obj, iteratee, context) {
if (obj == null) return {};
// mapValues returns an object and thus it doesn't make any sense for collections
if(obj.length === +obj.length) return _.map(obj,iteratee,context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this, we should always return an object by contract

In contrast to _.map it returns an object

You'll also need to test that case

@wilzbach
Copy link
Contributor Author

@megawac what is the expected behavior if you use this method with an array?

@megawac
Copy link
Collaborator

megawac commented Nov 30, 2014

Try doing Object.keys([1,2,3]), you'll notice no length attribute. It should return an object {0: <x>, 1: <y>, 2: <z>}

@wilzbach
Copy link
Contributor Author

@megawac corrected - thanks :)

results = {},
currentKey;
for (var index = 0; index < length; index++) {
currentKey = keys[index];
Copy link
Collaborator

Choose a reason for hiding this comment

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

var key = keys[index]

to follow current style + everything travis reports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sry for being so ignorant to the coding style warnings.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries LGTM after this last minor style change

Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks to be a copy of _.map's style, which uses currentKey.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a nearly 1:1 copy of _map, so I thought keeping the index variable name might make sense ...

@jridgewell
Copy link
Collaborator

👍

deepEqual(_.mapValues(obj, function(val) {
return val * 2;
}), {'a': 2, 'b': 4}, 'simple objects');

Copy link
Collaborator

Choose a reason for hiding this comment

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

mind adding a test for _.each([null, void 0, 1, "abc", [], {}], (val) => deepEqual(_.mapValues(val, _.identity), {}))

Also, mind adding a test for inherited values from prototype?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, mind adding a test for inherited values from prototype?

sth. like this?

var proto = function(){this.a = 1};
proto.prototype.b = 1;
var obj = new proto();
deepEqual(_.mapValues(obj, _.identity), {a: 1});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep!, also mind adding {} and [] to that empty list

@megawac
Copy link
Collaborator

megawac commented Dec 3, 2014

I'm +1 for this if you mind squashing your commits @greenify.

@wilzbach
Copy link
Contributor Author

wilzbach commented Dec 3, 2014

I'm +1 for this if you mind squashing your commits @greenify.

Of couse - sorry for being so noobish ...

@megawac
Copy link
Collaborator

megawac commented Jan 19, 2015

Closes #1869

@jashkenas
Copy link
Owner

This looks good to me. Go for it.

megawac added a commit that referenced this pull request Feb 6, 2015
Object-preserving map() function: mapValues
@megawac megawac merged commit 58113cf into jashkenas:master Feb 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants