-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement deepPluck
, a pluck operator for nested properties
#526
Conversation
I think it's worth mentioning that this implementation differs from I'd suggest simplifying the property resolving by using: return args.reduce(function(resolvedProperty, propertyName) {
return resolvedProperty === undefined && resolvedProperty || resolvedProperty[propertyName];
}, x); By the way, do not include files from the |
Also the operator could live in it's own file (modify |
Scratch the comment about using |
@sergi my concern with that is with pluck and why it's not implemented like that in any other library such as underscore, lo-dash, etc is the following: λ node
> var foo = {};
undefined
> foo['.bar'] = 42;
42
> foo
{ '.bar': 42 } As you can see, having a dot in front of a property name is perfectly valid, so by doing a deep pluck, I'd be missing said properties. |
@mattpodwysocki The proposed |
9987448
to
04a66b2
Compare
@urmastalimaa I made the changes you proposed, made sense. I thought of using @mattpodwysocki Yeah my first thought was doing it based on strings, but there were too many edge cases. As @urmastalimaa points out, it now takes |
@sergi The implementation is still buggy Rx.Observable.return({a: 0}).deepPluck('a').subscribe(console.log.bind(console))
// => undefined I suggest adding a falsy value to the tests. |
@urmastalimaa Thanks for the pointer, solved now. |
Minor micro-optimization I'll put out there as well, the length of the argument array can be pre-cached to avoid having it called (# of elements) * (# of arguments) times. Shouldn't really matter in Chrome and Firefox, but this will really bite you if you are using IE. |
@paulpdaniels thanks for the suggestion, I implemented it. |
@sergi @paulpdaniels should we just use this in place of |
I'm for it. |
@mattpodwysocki That makes sense. If everybody agrees I can update it accordingly then. |
@sergi I think it's agreed, DO IT FOR THE GREATER GOOD! |
@mattpodwysocki lol, done :) |
Implement `deepPluck`, a pluck operator for nested properties
I found myself implementing this operator time and again in my projects, since it is quite convenient. I thought it would be great to have it in the main RxJS distribution.
deepPluck
is likepluck
for nested properties. This would be an example of use:deepPluck
will returnundefined
for any property that can't be resolved, just likepluck
does.I included the rx generated files in the PR, please let me know if that's wrong.