Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,836 changes: 1,046 additions & 790 deletions docs/underscore.html

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
<li data-name="create">- <a href="#create">create</a></li>
<li data-name="object-functions" data-aliases="methods">- <a href="#object-functions">functions</a></li>
<li data-name="findKey">- <a href="#findKey">findKey</a></li>
<li data-name="findKeys">- <a href="#findKeys">findKeys</a></li>
<li data-name="extend">- <a href="#extend">extend</a></li>
<li data-name="extendOwn" data-aliases="assign">- <a href="#extendOwn">extendOwn</a></li>
<li data-name="pick">- <a href="#pick">pick</a></li>
Expand Down Expand Up @@ -1491,6 +1492,22 @@ <h2 id="objects">Object Functions</h2>
to facilitate shorthand syntaxes.
</p>

<p id="findKeys">
<b class="header">findKeys</b><code>_.findKeys(object, predicate, [context])</code>
<br />
Similar to <a href="#findKey"><tt>_.findKey</tt></a> but find all keys matching predicate in object.
Returns a new array containing all <i>keys</i> where the <b>predicate</b> truth test
passes.
<b>predicate</b> is transformed through <a href="#iteratee"><b>iteratee</b></a>
to facilitate shorthand syntaxes.
</p>
<pre>
_.findKeys({moe: 39.1, larry: 23.3, curly: 35.2}, function(value, key, object) {
return value > 30;
});
=&gt; ["moe", "curly"]
</pre>

<p id="extend">
<b class="header">extend</b><code>_.extend(destination, *sources)</code>
<br />
Expand Down Expand Up @@ -1970,7 +1987,7 @@ <h2 id="utility">Utility Functions</h2>
The following Underscore methods transform their predicates through
<tt>_.iteratee</tt>: <tt>countBy</tt>, <tt>every</tt>,
<tt>filter</tt>, <tt>find</tt>, <tt>findIndex</tt>, <tt>findKey</tt>,
<tt>findLastIndex</tt>, <tt>groupBy</tt>, <tt>indexBy</tt>,
<tt>findKeys</tt>, <tt>findLastIndex</tt>, <tt>groupBy</tt>, <tt>indexBy</tt>,
<tt>map</tt>, <tt>mapObject</tt>, <tt>max</tt>, <tt>min</tt>,
<tt>partition</tt>, <tt>reject</tt>, <tt>some</tt>, <tt>sortBy</tt>,
<tt>sortedIndex</tt>, and <tt>uniq</tt>
Expand Down
2 changes: 1 addition & 1 deletion test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
'findIndex', 'findLastIndex'
];
var object = [
'mapObject', 'findKey', 'pick', 'omit'
'mapObject', 'findKey', 'findKeys', 'pick', 'omit'
];

_.each(collection.concat(array), function(method) {
Expand Down
1 change: 1 addition & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@
assert.strictEqual(_.find(collection, /b/g), 'bar');
assert.strictEqual(_.findIndex(collection, /b/g), 1);
assert.strictEqual(_.findKey(collection, /b/g), '1');
assert.deepEqual(_.findKeys(collection, /b/g), ['1', '2']);
assert.strictEqual(_.findLastIndex(collection, /b/g), 2);
assert.deepEqual(_.groupBy(collection, /b/g), {0: ['foo'], 1: ['bar'], 2: ['bbiz']});
assert.deepEqual(_.indexBy(collection, /b/g), {0: 'foo', 1: 'bar', 2: 'bbiz'});
Expand Down
41 changes: 41 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,47 @@
assert.strictEqual(_.findKey(array, function(x) { return x === 55; }), 'match', 'matches array-likes keys');
});

QUnit.test('findKeys', function(assert) {
var objects = {
a: {a: 0, b: 0},
b: {a: 1, b: 1},
c: {a: 2, b: 2},
d: {a: 1, b: 4}
};

assert.deepEqual(_.findKeys(objects, function(obj) {
return obj.a === 0;
}), ['a']);

assert.deepEqual(_.findKeys(objects, function(obj) {
return obj.b * obj.a === 4;
}), ['c', 'd']);

assert.deepEqual(_.findKeys(objects, 'a'), ['b', 'c', 'd'], 'Uses lookupIterator');

assert.deepEqual(_.findKeys(objects, function(obj) {
return obj.b * obj.a === 5;
}), []);

assert.deepEqual(_.findKeys([1, 2, 3, 4, 5, 6], function(obj) {
return obj === 3;
}), ['2'], 'Keys are strings');

assert.deepEqual(_.findKeys(objects, function(a) {
return a.foo === null;
}), []);

_.findKeys({a: {a: 1}}, function(a, key, obj) {
assert.strictEqual(key, 'a');
assert.deepEqual(obj, {a: {a: 1}});
assert.strictEqual(this, objects, 'called with context');
}, objects);

var array = [1, 2, 3, 4];
array.match = 55;
assert.deepEqual(_.findKeys(array, function(x) { return x === 55; }), ['match'], 'matches array-likes keys');
});


QUnit.test('mapObject', function(assert) {
var obj = {a: 1, b: 2};
Expand Down
6 changes: 3 additions & 3 deletions underscore-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion underscore-min.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,16 @@
}
};

// Returns an array of all keys on an object that pass a predicate test.
_.findKeys = function(obj, predicate, context) {
predicate = cb(predicate, context);
var allKeys = _.keys(obj), res = [];
_.each(allKeys, function(key) {
if (predicate(obj[key], key, obj)) res.push(key);
});
return res;
};

// Internal pick helper function to determine if `obj` has key `key`.
var keyInObj = function(value, key, obj) {
return key in obj;
Expand Down