-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Abstract iterator binding to createIterator and callbacks to createCallback #1582
Conversation
@braddunbar -- When you have a free moment, I'd be curious to hear what you think of this patch... |
|
||
//can be used like findwhere | ||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b: 4}]; | ||
var result = _.find(list, {a: 1}, 'can be used as findWhere'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this message goes on the following line (not as the context argument to _.find
).
After mulling it over, I'm for this one. I like the property sugar and while I'm not particularly fond of the |
|
||
function test() {} | ||
test.map = _.map; | ||
deepEqual(_.where([_, {a: 1, b: 2}, _], test), [_, _], 'checks properties given function'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this test as functions are just another object as far as _.where
is concerned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not, but its a gotcha I had in a previous commit (4512921) which is why I added a test for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough! :)
As #1609 noted that |
Whoops, I forgot to add tests for max and min but they both will benefit from lookup iterator. |
Mind rebasing this so that it merges in cleanly? |
Long coming optimization discussed in 1525, 1561 and elsewhere. Follows lodash's lead.
See included tests and lodash docs for some examples LookupIterators maps string properties (see jashkenas#1557) and objects to _.matches. As a result several of the helper functions such as _.where, _.pluck, _.findWhere are handled by their parent implementations.
As discussed in PR with jdalton and @bradunbar
@jashkenas rebase is done |
Abstract iterator binding to createIterator and callbacks to createCallback
I believe this test was added by accident by @megawac in jashkenas#1582 (specifically ca404d40f1705ffc84c5cd8a8a95a04373df2c76) Given our current documentation, the behavior of this edge case is not defined. This test currently passes because arrays are technically objects and thus `_.iteratee` treats `[]` as a `_.matches` matchers. `_.matches` casts `[]` to `{}` via `_.extendOwn({}, []);` and returns a function witch always returns `true`. The fact that this test exists in `_.reject`'s test and not in `_.iteratee`'s leads me to believe that this is not actually intended behavior, but merely an implementation detail which has been accidentally enforced in our tests. I'm currently reworking my pull request jashkenas#2494 (deep property access) and in a future where arrays are used for deep property access, `[]` will return a function that always returns `undefined`, which seems like a more reasonable behavior. I thought it would be good to have this conversation separate from the deep property matching pull request, but removing this test is a prerequisite to implementing deep property access.
Highlights
.call()
and instead calls the function directly whenever possibletoLookupIterator
to_.every
,_.some
,_.map
,_.filter
#1561):lookupIterator
maps passed strings to_.property
(see Avoid Function#call unless context is provided. #1557) and objects to_.matches
allowing for some functionality thats existed in lodash for some time. It also allows_.find
to cover_.findWhere
,_.filter
to cover_.where
,_map
to cover_.pluck
and essentially adds_.rejectWhere
as_.reject
.There are many side effects of this commit and so I can go grab dinner I'm going to pull examples from lodash docs (see included tests for more examples):