-
Notifications
You must be signed in to change notification settings - Fork 217
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
Add 'forEachNode' function #45
Conversation
Looks great! Would love to see it used in combination with my json-operator |
@dchester Can you please review it? |
I will hope to take a closer look this evening, but quick thoughts from a glance:
|
As discussed in #41
@dchester Thanks for the feedback.
It depends on your standpoint on introducing breaking changes.
In my use case, I frequently need to remove/update parent node and sometimes parent of parents. node.parent().update(null); //replace parent with null
node.parent(1).remove(); //remove parent of parent I can see only donwside is that we need to create new _NodeWrapper object and copy of path array using |
I like this proposal, perhaps even make it configurable via factory function as an option? I tend to do that quite a lot... "I can convert jp.nodes to return class Node with public path/value attributes instead of functions" |
@dchester I implement suggestions from my last comment as separate commit. |
Sorry for the delay -- I'm travelling now but will have a chance to take a look tomorrow evening. |
sure |
So, after digging in here, I'm wondering if we should just stick with a procedural interface across the board. I do like how the object-oriented approach feels specifically for the use case of deleting nodes, but I think it may be more complicated than we want/need to have the two different paradigms co-existing, and I'd rather keep backwards compatibility. Given that, what do you think about an interface that would work something like this? Delete a single node, given a path: jp.delete(obj, '$.authors[0]') And a helper to iterate through all nodes and delete the ones where the supplied callback evaluates to true. jp.prune(obj, value => value.name == 'rogers'); Or alternatively, for more control, for example to delete the parent node, jp.nodes(obj, '$..*').forEach(n => {
if (n.value.name == 'rogers') jp.delete(obj, jp.up(path));
}); In some ways this is less elegant and more verbose, but I like how it is consistent with the approach taken by the rest of the library. Does something in this direction solve the use-cases we're looking to solve? |
Beginnings/explorations of the |
Sounds good to me :) |
@dchester IMHO As for The second problem is that To provide some background for my comments, my use-case involves the complicated transformation of JSON. Here is an example: https://github.com/APIs-guru/raml-to-swagger/blob/master/src/index.js#L409 |
Thanks for the example use case -- that helps. I see how the object-model approach simplifies those somewhat complicated transformations, where it would be useful to actually traverse up and down the data structure, etc. That said, as I mentioned before, I am reluctant to mix paradigms and to expand the scope of this library, so I like your idea to have the Node wrapper functionality live as its own project (which, for what it's worth, I'd be happy to help with). Good point also, about the array of path components being a potentially confusing interface for those helper methods. We should be able to make those methods accept strings as an alternative to path components, which be less surprising, at least. |
Closing stale PR. |
Implemented as discussed in #41
@dchester @kristianmandrup What do you think?