_.pick and _.omit accept a predicate #1485

Merged
merged 7 commits into from Mar 4, 2014

Projects

None yet

5 participants

@braddunbar
Collaborator

Based on #1472. Tweaked to branch for iterator case and cleaned up tests.

@braddunbar
Collaborator

Also, updated documentation in e991e75.

@davidchambers davidchambers and 1 other commented on an outdated diff Feb 17, 2014
@@ -1307,25 +1307,35 @@ <h2 id="objects">Object Functions</h2>
</pre>
<p id="pick">
- <b class="header">pick</b><code>_.pick(object, *keys)</code>
+ <b class="header">pick</b><code>_.pick(object, [predicate|*keys], [context])</code>
@davidchambers
davidchambers Feb 17, 2014 Contributor

Should this read _.pick(object, [*keys | predicate, [context]])? Confusing, perhaps, but more accurate.

@braddunbar
braddunbar Feb 17, 2014 Collaborator

I thought about that but it looks so confusing (both ways do). I'm leaning towards reverting to *keys and leaving the predicate documentation as prose only.

@davidchambers
davidchambers Feb 17, 2014 Contributor

That might be the best option. Reading (object, *keys, [context]) is confusing, as one sees a variadic function with an optional argument and wonders whether the last argument will be treated as a key or as the context.

@davidchambers davidchambers and 1 other commented on an outdated diff Feb 17, 2014
</p>
<pre>
_.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
=&gt; {name: 'moe', age: 50}
+_.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object){
@davidchambers
davidchambers Feb 17, 2014 Contributor

s/{$/ {/

@braddunbar
braddunbar Feb 17, 2014 Collaborator

Thanks! Updated.

@jdalton
Contributor
jdalton commented Feb 25, 2014

👍 I've had an implementation of this for a year and a half ( using _.omit and _.pick) without issue.

@jashkenas
Owner

After this change, both pick and omit are largely the same function body. Any way to boil that down into a single internal function that both make use of?

@braddunbar
Collaborator

Taking a second look, I don't think these are as similar as they seem. Any attempt I made to combine them ended up being way to clever for my taste. Feel free to tweak though. :)

@jashkenas jashkenas merged commit 0d0baa3 into jashkenas:master Mar 4, 2014
@braddunbar braddunbar deleted the braddunbar:pick-omit branch Mar 4, 2014
@braddunbar braddunbar referenced this pull request Mar 17, 2014
Closed

Filtering objects #1523

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment