-
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
Updated dependencies and introduced delete option with test #41
base: master
Are you sure you want to change the base?
Conversation
@@ -166,6 +166,8 @@ var nodes = jp.apply(data, '$..author', function(value) { return value.toUpperCa | |||
// ] | |||
``` | |||
|
|||
If the supplied application function returns the special value `:delete:` the node will be deleted and not updated as is otherwise the default case. |
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.
@kristianmandrup Why do you need special :delete:
value you can simply detect when callback returns undefined
.
- What if I want to replace the value with literal
:delete:
.
IMHO, it's bad practice to create magic values.
"underscore": "1.7.0" | ||
"esprima": "^2.0.0", | ||
"jison": "^0.4.17", | ||
"lodash.filter": "^4.6.0", |
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.
@kristianmandrup I don't see where filter
is used?
See in |
See test example I'm using this version in json-operator where I really need a good delete option. |
I agree it might be nice to support conditionally removing nodes based on a supplied callback function, but do we need to overload Either way, I agree with @IvanGoncharov that we should be able to find an approach where special strings are not part of the solution. Even But can we just skip all of that by implementing |
Sounds good with |
Even better solution would be to simply add a second argument Something like this, except how about
Ah, of course, now I see, then key will be |
Added new conditional filter method with tests and Readme update. Added context object to callback |
@kristianmandrup @dchester I think after
How about more generic solution? for (let node of jp.iterable('$..b.c')) {
if (node.value() === '')
node.remove();
} If you want more advanced operations you can use any lib that support |
Sounds great!! :) So far my fork supports the new I'm all for a more flexible/generic solution like you propose... then I will change my |
@kristianmandrup @dchester jp.forEachNode('$..b.c', function (node) {
//remove all empty strings
if (node.value() === '')
node.delete();
}); |
Sure, looks sensible :) I'm not aware of the node API you are using. Would be nice to be documented. |
@kristianmandrup Yes, they are. BTW. Can you please split up dependency update into separate PR. |
"Yes they are." Documented where?? just been browsing original article and your code, couldn't find any mention of the node api, such as |
Yes it is of part of my proposal to create this wrapper class: function Node (parent, index) {
this.parent = parent;
this.index = index;
}
Node.prototype.value = function () {
return this.parent[this.index];
}
Node.prototype.remove = function () {
if (Array.isArray(this.parent))
Array.prototype.splice.call(this.parent, parseInt(this.index), 1);
else
delete this.parent[this.index];
}
// Other methods And use it as a parameter to the callback in |
Wow! Looks perfect :) |
Adding delete seems interesting; what is the status please? |
bump! Would love this functionality |
All tests pass. Note, when trying to upgrade to Esprima 3:
So the
aesprima
hack, trying to inject@
as a valid identifier fails. Likely because it is now a valid symbol for decorators? So I had to downgrade to esprima 2 to make it work. Still better than before ;)