-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add proper Object function matches #2057
Conversation
if (arguments.length < 2) return _.matcher(object); | ||
var keys = _.keys(attrs), length = keys.length; | ||
if (object == null) return !length; | ||
var obj = Object(object); |
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.
Note: I'm intentionally not assigning to object
here to avoid an arguments deopt.
lodash has _.isMatch it'd be nice to align to that and avoid overloading |
Sorry, I meant to link #1985 as well. I overloaded |
This takes `_.matches` and turns it into a proper Object function (takes the object as the first parameter). The should satisfy the performance problems seen in [jashkenas/backbone#3487](jashkenas/backbone#348 7#issuecomment-75001423). I think it’s pretty apparent we jumbled the arguments originally. See the need to special case [`Backbone.Model#matches`](https://github.com/jashkenas/backbone/blob/ma ster/backbone.js#L390-L393) instead of auto proxying like the other [`_` methods](https://github.com/jashkenas/backbone/blob/master/backbone.js#L 669-L680). We even have to special case [`Collection#matches`](https://github.com/jashkenas/backbone/blob/master /backbone.js#L917-L924) so we don’t kill performance doing simple `where`s. The legacy `_.matches(obj)` predicate matcher has been extracted into `_.matcher`. `_.matches` will return the predicate matcher if only one argument is provided. This legacy support should be removed in v2.0.
4a99624
to
6d3100e
Compare
Very nice.
|
Add proper Object function matches
|
Yeah, I agree with @jdalton I think it makes sense for this to be This will actually break some suggestions I've made on SO and the issues as |
Ya :( |
But it would make the Backbone method ugly.
Could be aliased?
We can add a guard here, and remove that with v2.0 as well. I just really dislike having |
I feel like that is more effort than its worth: I'd say just leave matches be |
I've gotten used to it. It makes sense since it produces a function that checks if an object matches the source. This change feels inconsistent since there's |
@megawac: how were you using @jashkenas: Thoughts on overloading? |
@jridgewell it was something like var obj = {a: 1};
var hasMatch = _.chain([{b: 1}, 'a']).map(_.iteratee).any((matcher) => matcher(obj)).value(); I don't recall exactly what the code was |
Reads nicely. The overloading is certainly gross, but can be removed over time. Leaving the function named poorly would be worse in the long term. |
@jashkenas I disagree, a poorly named function is much better than changing the contract. This will force some users using |
Naming is hard and it can be seen to work as both so
I'd argue that it doesn't read as nicely because devs are used to a different behavior (with a single arg). _.matches(source);
_.matches(object, source); Now, sometimes the first |
Yea, returning a function or a boolean with an optional first argument, not second, is about an confusing as it gets. If we dislike the name |
I don't think any breaking change was introduced. @megawac mentioned guarded behavior but you would never use The optional first arg is clunky, but it'll go away in the next major (hopefully) release. |
@jridgewell here's a (currently non working) example of something that would break with this
|
Damn. I'll work a |
👍 The current way this is being implemented wastes LOC, simplicity and introduces breaking risks for a name. Just implement |
Alright then — @jridgewell — you got this? |
Yah, I'll get a new PR tonight. Can we get a 2.0 milestone with |
Ah, I was thinking about sooner than that ;) I'll get it. Re: 2.0 — nah. A little naming jiggery-pokery isn't a good enough reason to start rolling that ball down the hill. |
How about pick the grammatically correct |
This takes
_.matches
and turns it into a proper Object Function (takes the object as the first parameter). This should satisfy the performance problems seen in jashkenas/backbone#3487.I think it’s pretty apparent we jumbled the arguments originally. See the need to special case
Backbone.Model#matches
instead of auto proxying like the other_
methods. We even have to special caseCollection#matches
so we don’t kill performance doing simplewhere
s.The legacy
_.matches(obj)
predicate matcher has been extracted into_.matcher
._.matches
will return the predicate matcher if only one argument is provided. This legacy support should be removed in v2.0.