[Request for feedback] Add deep property access via dot notation#2638
[Request for feedback] Add deep property access via dot notation#2638captbaritone wants to merge 2 commits intojashkenas:masterfrom
Conversation
|
Thoughts @jridgewell @jashkenas @megawac? |
|
I don't love string paths in JavaScript, but this looks fine to me — without the fancy dotted-path escaping. |
|
I like the proposal for array style paths as they are very explicit, but I don't like the concept of partial string parsing. I think if we support string paths we should be able to parse the complete javascript path ast. I would prefer to have users can convert their string paths to array paths using a multitude of third party libraries or a simple approach as in this code using split. We can also consider leaving a hook so users can override the behaviour of the path parsing P.S. this isn't too strong of an opinion, I just don't want this to be a can of worms of things to support in the future. |
Very much agreed.
What is the "complete javascript path ast"? |
|
Didn't make that very clear, but complicated paths rules like Here's a couple examples from underscore-contrib https://github.com/documentcloud/underscore-contrib/blob/master/test/object.selectors.js#L30-L109 |
Expose our path parsing function both for external use and for customization. This name was chosen for compatibility with lodash.
|
I like @megawac's idea of allowing users to customize path parsing so I've added a commit which exposes our path parsing function as I think we probably should support complex paths. Although like both of you, I don't feel terribly strongly about it, especially given:
Maybe we can merge this PR as is and I'll follow-up with a PR that adds complex path parsing? Then we can decide if the performance/weight/complexity of the implementation is worth the added convenience? |
|
I think that I disagree pretty strongly with exposing That's the kind of fiddly detail that makes Underscore more difficult to understand, more difficult to use, and more difficult to share code / examples across different projects that override If we're going to support string paths, we just just do the right thing. Maybe that's a simple path shortcut that's not supposed to handle all of the complex cases, maybe that's comprehensive path parsing — but we shouldn't be pushing the conceptual complexity on to the user. |
|
I suppose there are really two things here:
I don't see any reason not to do number 1. We already have it, and it may be useful to someone else. As for number two, I think we did set some precedence for this type of customization in #2480 where we allowed users to supply their own custom I would agree with @jashkenas that customizing these methods is probably an anti-pattern. It makes your code behave differently than documented. Especially on a project with multiple engineers, customization would probably be a bad choice. We should not be in the position of encouraging people to make bad choices. Then again, we're all adults here, why not let people make that tradeoff for themselves? It does serve as a nice pressure release for the maintainers. We cannot be all things to all people, and features like I realize I'm getting a bit philosophical here, and not actually forming a helpful opinions, so how about this: We add support for complex paths. I think we can all see the value in it. With that in place I don't see any rational need to customize |
That's a fair proposal, but then we get into a different discussion: What exactly do the complex paths look like? What's the supporting parsing code required to handle them robustly? Again, reiterating my mild opinion that string paths in JavaScript are distasteful, we should weigh the pros and cons of the three options:
|
|
I believe that there's an option between point 2 and point 3, where a reasonable subset of paths are supported, and anything more complicated can be done another way. Personally, I only want dot paths, and brackets for arrays. So, |
That complicates things. Why do you need |
|
Because this is how people think when descending their data structures. Would you be amenable to this if I wrote a toPath implementation that handied it well? |
Sure! |
|
@bjmiller Could it be as simple as stripping out the surrounding |
|
Not that simple, but probably not an order of magnitude more complex, either. |
|
@bjmiller Any timeline on when you would be able to get to this? |
|
I've done some work on it, I'm hoping to finish up soon. Perhaps this weekend? Let me know if there's a date you're trying to get this by. I think I'm going to put up a gist with my approach first, and I'll make a proper PR out of it with tests and all if it's not rejected out of hand. |
|
No specific timeline, but I wanted to make sure we're maintaining some sort of momentum.
…--
Jordan Eldredge (mobile)
On Jan 25, 2017, at 9:26 PM, Brian Miller ***@***.***> wrote:
I've done some work on it, I'm hoping to finish up soon. Perhaps this weekend? Let me know if there's a date you're trying to get this by.
I think I'm going to put up a gist with my approach first, and I'll make a proper PR out of it with tests and all if it's not rejected out of hand.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
Here's my first draft, in a gist: https://gist.github.com/bjmiller/be6a391e1f766ffe7010d3384ef74a2e Let me know what you think. If you all hate it, I'll go a different way. |
|
Fast-forward nearly four years, and it turns out we went with a customizable public |
|
@jgonggrijp I believe it does not fix this issue it just ignores a choice of deep property access via dot notation... P.S. I like that it has _.get now, but I would prefer deepProperty access via string path. |
|
@Dexmaster you can override |
It's not the best but ok, and it's just I'm not bashing underscore.js it's a good lib, but it can add some useful parts. P.S. For comparison from 2016 I was using lodash, which was based on Underscore.js, but had much more useful |
|
It's easier to use another lib, than fix one (create overrides), for every project you need it in... |
If you see a way to improve on it, please feel welcome to submit a PR.
If you feel that Underscore is broken, then by all means use another library. But consider the other side: if we were to change all functions that process paths to split dotted strings, then all code like the following will break: _.property('underscorejs.org')({
'underscorejs.org': '2020-11-24',
'npmjs.com': '2020-11-25'
});As I see it, giving people a choice between array paths and string paths is the best we can do. Given that only one of the options can be the default, I think array paths are the safer option; it is more general and it doesn't break any code. |
Works just fine... |
|
And I'm not saying it should be perfect, I just mentioned to add a really good feature, that people will use in 4 years is not hard, even if it will be same as in ommitPath split('.') and that's it... This PR was just simple if string split('.'), I mentioned two years ago how it can be improved if you wanted square brackets too... but it still would have worked, can't tell you more. I said my thoughts on this subject. |
No, it is not, as in other libraries, you can provide multiple choices for people to select. It's much easier to save path string on a server and just use as is in As I said it's 4 years old PR, and it's spilt milk for all I care, I understand it wasn't prioritized, I just wanted to tell you your arguments are not working here. I'm glad you want to find a reason to justify closing this PR, but all the ones you've provided are not working.
I'm not saying underscore is broken. I said that 4 years ago I had a need to get a value from a complex object that was optionally appearing in some users, and to get it via I'm saying this feature was not hard to approve, I don't see a reason why it was not approved till now. And the reason was not provided here... And one that was provided I gave a solution, but it was still rejected without concrete rime or reason... P.S. I'm not the creator of this PR, I still don't like how it ended... |
|
@Dexmaster I think you're missing my point, and I might be missing yours as well. I recognize that you feel strongly about this. If you like, we can take this to a private chat to ensure we fully understand each other. Edit: I'm on Gitter if that works for you. |

This pull request should be safe to merge, but it lacks support for escaping characters in path strings. In most cases this is fine, since the more explicit array syntax can be used as a fallback. However, in the case of
_.matchesand friends, there is no such fallback available.Should we explore an escape syntax (similar to Lodash's) which allows for complex paths via a string syntax? If so, should it be solved as part of this pull request?
As part of this pull request we get
_.getessentially for free.