Skip to content

Commit

Permalink
/s/mapValues/mapObject and so on...
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 19, 2015
1 parent 730a80b commit 48f005f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 57 deletions.
56 changes: 12 additions & 44 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@
<li>- <a href="#map">map</a></li>
<li>- <a href="#reduce">reduce</a></li>
<li>- <a href="#reduceRight">reduceRight</a></li>
<li>- <a href="#transform">transform</a></li>
<li>- <a href="#find">find</a></li>
<li>- <a href="#filter">filter</a></li>
<li>- <a href="#where">where</a></li>
Expand Down Expand Up @@ -277,7 +276,7 @@
<li>- <a href="#keys">keys</a></li>
<li>- <a href="#keysIn">keysIn</a></li>
<li>- <a href="#values">values</a></li>
<li>- <a href="#mapValues">mapValues</a></li>
<li>- <a href="#mapObject">mapObject</a></li>
<li>- <a href="#pairs">pairs</a></li>
<li>- <a href="#invert">invert</a></li>
<li>- <a href="#object-functions">functions</a></li>
Expand Down Expand Up @@ -523,28 +522,6 @@ <h2 id="collections">Collection Functions (Arrays or Objects)</h2>
var list = [[0, 1], [2, 3], [4, 5]];
var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
=&gt; [4, 5, 2, 3, 0, 1]
</pre>

<p id="transform">
<b class="header">transform</b><code>_.transform(collection, iteratee, [memo], [context])</code>
<br />
<tt>_.transform</tt> is an alternative to <tt>_.reduce</tt> that transforms
the given collection into a new collection. Unlike <tt>_.reduce</tt> if the
memo isn't specified it will default to a new instance of the type of the collection.
Another distinction is that the memo cannot be changed, which means returning
the memo isn't necessary.
</p>
<pre>
_.transform(['bob', 'nick', 'rick'], function(memo, name) {
memo[name] = true;
}, {});
=&gt; {'bob': true, 'nick': true, 'rick': true}

// Note: transform guesses the type you want if you don't specify!
_.transform({'a': 3, 'b': 4}, function(memo, val, key) {
memo[key] = Math.pow(2, val);
});
=&gt; {'a': 8, 'b': 16}
</pre>

<p id="find">
Expand Down Expand Up @@ -1066,15 +1043,6 @@ <h2 id="arrays">Array Functions</h2>
where the <strong>predicate</strong> truth test passes; otherwise returns <i>-1</i>.
</p>
<pre>
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
_.findIndex([4, 6, 8, 12], isPrime);
=&gt; -1 // not found
_.findIndex([4, 6, 7, 12], isPrime);
Expand Down Expand Up @@ -1375,18 +1343,18 @@ <h2 id="objects">Object Functions</h2>
=&gt; ["one", "two", "three"]
</pre>

<p id="keysIn">
<b class="header">keysIn</b><code>_.keysIn(object)</code>
<p id="allKeys">
<b class="header">allKeys</b><code>_.allKeys(object)</code>
<br />
Retrieve all the inheritted and own enumerable properties of an <b>object</b>.
Retrieve <i>all</i> the names of <b>object</b>'s own and inherited properties.
</p>
<pre>
function Class() {
this.a = 5;
function Stooge(name) {
this.name = name;
}
Class.prototype.b = 1;
_.keysIn(new Class());
=&gt; ["a", "b"]
Stooge.prototype.silly = true;
_.allKeys(new Stooge("Moe"));
=&gt; ["name", "silly"]
</pre>

<p id="values">
Expand All @@ -1399,14 +1367,14 @@ <h2 id="objects">Object Functions</h2>
=&gt; [1, 2, 3]
</pre>

<p id="mapValues">
<b class="header">mapValues</b><code>_.mapValues(array, iteratee, [context])</code>
<p id="mapObject">
<b class="header">mapObject</b><code>_.mapObject(array, iteratee, [context])</code>
<br />
Iterates an object and returns a new object with the values computed by
the given <tt>iteratee</tt>.
</p>
<pre>
_.mapValues({a: 1, b: 2}, function(val, key) {
_.mapObject({a: 1, b: 2}, function(val, key) {
return val * 2;
});
=> {a: 2, b: 4}
Expand Down
2 changes: 1 addition & 1 deletion test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
'findIndex', 'findLastIndex'
];
var object = [
'mapValues', 'findKey', 'pick', 'omit'
'mapObject', 'findKey', 'pick', 'omit'
];

_.each(collection.concat(array), function(method) {
Expand Down
22 changes: 11 additions & 11 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,57 +933,57 @@
});


test('mapValues', function() {
test('mapObject', function() {
var obj = {'a': 1, 'b': 2};
var objects = {
a: {'a': 0, 'b': 0},
b: {'a': 1, 'b': 1},
c: {'a': 2, 'b': 2}
};

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

deepEqual(_.mapValues(objects, function(val) {
deepEqual(_.mapObject(objects, function(val) {
return _.reduce(val, function(memo,v){
return memo + v;
},0);
}), {'a': 0, 'b': 2, 'c': 4}, 'nested objects');

deepEqual(_.mapValues(obj, function(val,key,obj) {
deepEqual(_.mapObject(obj, function(val,key,obj) {
return obj[key] * 2;
}), {'a': 2, 'b': 4}, 'correct keys');

deepEqual(_.mapValues([1,2], function(val) {
deepEqual(_.mapObject([1,2], function(val) {
return val * 2;
}), {'0': 2, '1': 4}, 'check behavior for arrays');

deepEqual(_.mapValues(obj, function(val) {
deepEqual(_.mapObject(obj, function(val) {
return val * this.multiplier;
}, {multiplier : 3}), {'a': 3, 'b': 6}, 'keep context');

deepEqual(_.mapValues({a: 1}, function() {
deepEqual(_.mapObject({a: 1}, function() {
return this.length;
}, [1,2]), {'a': 2}, 'called with context');

var ids = _.mapValues({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){
var ids = _.mapObject({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){
return n.id;
});
deepEqual(ids, {'length': undefined, '0': '1', '1': '2'}, 'Check with array-like objects');

// Passing a property name like _.pluck.
var people = {'a': {name : 'moe', age : 30}, 'b': {name : 'curly', age : 50}};
deepEqual(_.mapValues(people, 'name'), {'a': 'moe', 'b': 'curly'}, 'predicate string map to object properties');
deepEqual(_.mapObject(people, 'name'), {'a': 'moe', 'b': 'curly'}, 'predicate string map to object properties');

_.each([null, void 0, 1, 'abc', [], {}, undefined], function(val){
deepEqual(_.mapValues(val, _.identity), {}, 'mapValue identity');
deepEqual(_.mapObject(val, _.identity), {}, 'mapValue identity');
});

var Proto = function(){this.a = 1;};
Proto.prototype.b = 1;
var protoObj = new Proto();
deepEqual(_.mapValues(protoObj, _.identity), {a: 1}, 'ignore inherited values from prototypes');
deepEqual(_.mapObject(protoObj, _.identity), {a: 1}, 'ignore inherited values from prototypes');

});
}());
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@

// Returns the results of applying the iteratee to each element of the object
// In contrast to _.map it returns an object
_.mapValues = function(obj, iteratee, context) {
_.mapObject = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
var keys = _.keys(obj),
length = keys.length,
Expand Down

0 comments on commit 48f005f

Please sign in to comment.