Skip to content

Commit 9d66959

Browse files
committed
Add new function findKeys
1 parent 20e7c6e commit 9d66959

8 files changed

+1121
-796
lines changed

docs/underscore.html

Lines changed: 1046 additions & 790 deletions
Large diffs are not rendered by default.

index.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
<li data-name="create">- <a href="#create">create</a></li>
296296
<li data-name="object-functions" data-aliases="methods">- <a href="#object-functions">functions</a></li>
297297
<li data-name="findKey">- <a href="#findKey">findKey</a></li>
298+
<li data-name="findKeys">- <a href="#findKeys">findKeys</a></li>
298299
<li data-name="extend">- <a href="#extend">extend</a></li>
299300
<li data-name="extendOwn" data-aliases="assign">- <a href="#extendOwn">extendOwn</a></li>
300301
<li data-name="pick">- <a href="#pick">pick</a></li>
@@ -1491,6 +1492,22 @@ <h2 id="objects">Object Functions</h2>
14911492
to facilitate shorthand syntaxes.
14921493
</p>
14931494

1495+
<p id="findKeys">
1496+
<b class="header">findKeys</b><code>_.findKeys(object, predicate, [context])</code>
1497+
<br />
1498+
Similar to <a href="#findKey"><tt>_.findKey</tt></a> but find all keys matching predicate in object.
1499+
Returns a new array containing all <i>keys</i> where the <b>predicate</b> truth test
1500+
passes.
1501+
<b>predicate</b> is transformed through <a href="#iteratee"><b>iteratee</b></a>
1502+
to facilitate shorthand syntaxes.
1503+
</p>
1504+
<pre>
1505+
_.findKeys({moe: 39.1, larry: 23.3, curly: 35.2}, function(value, key, object) {
1506+
return value > 30;
1507+
});
1508+
=&gt; ["moe", "curly"]
1509+
</pre>
1510+
14941511
<p id="extend">
14951512
<b class="header">extend</b><code>_.extend(destination, *sources)</code>
14961513
<br />
@@ -1970,7 +1987,7 @@ <h2 id="utility">Utility Functions</h2>
19701987
The following Underscore methods transform their predicates through
19711988
<tt>_.iteratee</tt>: <tt>countBy</tt>, <tt>every</tt>,
19721989
<tt>filter</tt>, <tt>find</tt>, <tt>findIndex</tt>, <tt>findKey</tt>,
1973-
<tt>findLastIndex</tt>, <tt>groupBy</tt>, <tt>indexBy</tt>,
1990+
<tt>findKeys</tt>, <tt>findLastIndex</tt>, <tt>groupBy</tt>, <tt>indexBy</tt>,
19741991
<tt>map</tt>, <tt>mapObject</tt>, <tt>max</tt>, <tt>min</tt>,
19751992
<tt>partition</tt>, <tt>reject</tt>, <tt>some</tt>, <tt>sortBy</tt>,
19761993
<tt>sortedIndex</tt>, and <tt>uniq</tt>

test/collections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
'findIndex', 'findLastIndex'
112112
];
113113
var object = [
114-
'mapObject', 'findKey', 'pick', 'omit'
114+
'mapObject', 'findKey', 'findKeys', 'pick', 'omit'
115115
];
116116

117117
_.each(collection.concat(array), function(method) {

test/functions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@
718718
assert.strictEqual(_.find(collection, /b/g), 'bar');
719719
assert.strictEqual(_.findIndex(collection, /b/g), 1);
720720
assert.strictEqual(_.findKey(collection, /b/g), '1');
721+
assert.deepEqual(_.findKeys(collection, /b/g), ['1', '2']);
721722
assert.strictEqual(_.findLastIndex(collection, /b/g), 2);
722723
assert.deepEqual(_.groupBy(collection, /b/g), {0: ['foo'], 1: ['bar'], 2: ['bbiz']});
723724
assert.deepEqual(_.indexBy(collection, /b/g), {0: 'foo', 1: 'bar', 2: 'bbiz'});

test/objects.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,47 @@
10971097
assert.strictEqual(_.findKey(array, function(x) { return x === 55; }), 'match', 'matches array-likes keys');
10981098
});
10991099

1100+
QUnit.test('findKeys', function(assert) {
1101+
var objects = {
1102+
a: {a: 0, b: 0},
1103+
b: {a: 1, b: 1},
1104+
c: {a: 2, b: 2},
1105+
d: {a: 1, b: 4}
1106+
};
1107+
1108+
assert.deepEqual(_.findKeys(objects, function(obj) {
1109+
return obj.a === 0;
1110+
}), ['a']);
1111+
1112+
assert.deepEqual(_.findKeys(objects, function(obj) {
1113+
return obj.b * obj.a === 4;
1114+
}), ['c', 'd']);
1115+
1116+
assert.deepEqual(_.findKeys(objects, 'a'), ['b', 'c', 'd'], 'Uses lookupIterator');
1117+
1118+
assert.deepEqual(_.findKeys(objects, function(obj) {
1119+
return obj.b * obj.a === 5;
1120+
}), []);
1121+
1122+
assert.deepEqual(_.findKeys([1, 2, 3, 4, 5, 6], function(obj) {
1123+
return obj === 3;
1124+
}), ['2'], 'Keys are strings');
1125+
1126+
assert.deepEqual(_.findKeys(objects, function(a) {
1127+
return a.foo === null;
1128+
}), []);
1129+
1130+
_.findKeys({a: {a: 1}}, function(a, key, obj) {
1131+
assert.strictEqual(key, 'a');
1132+
assert.deepEqual(obj, {a: {a: 1}});
1133+
assert.strictEqual(this, objects, 'called with context');
1134+
}, objects);
1135+
1136+
var array = [1, 2, 3, 4];
1137+
array.match = 55;
1138+
assert.deepEqual(_.findKeys(array, function(x) { return x === 55; }), ['match'], 'matches array-likes keys');
1139+
});
1140+
11001141

11011142
QUnit.test('mapObject', function(assert) {
11021143
var obj = {a: 1, b: 2};

underscore-min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore-min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,16 @@
11001100
}
11011101
};
11021102

1103+
// Returns an array of all keys on an object that pass a predicate test.
1104+
_.findKeys = function(obj, predicate, context) {
1105+
predicate = cb(predicate, context);
1106+
var allKeys = _.keys(obj), res = [];
1107+
_.each(allKeys, function(key) {
1108+
if (predicate(obj[key], key, obj)) res.push(key);
1109+
});
1110+
return res;
1111+
};
1112+
11031113
// Internal pick helper function to determine if `obj` has key `key`.
11041114
var keyInObj = function(value, key, obj) {
11051115
return key in obj;

0 commit comments

Comments
 (0)