Updated dependencies and introduced delete option with test#41
Updated dependencies and introduced delete option with test#41kristianmandrup wants to merge 5 commits intodchester:masterfrom
Conversation
README.md
Outdated
| // ] | ||
| ``` | ||
|
|
||
| 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.
@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.
package.json
Outdated
| "underscore": "1.7.0" | ||
| "esprima": "^2.0.0", | ||
| "jison": "^0.4.17", | ||
| "lodash.filter": "^4.6.0", |
There was a problem hiding this comment.
@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 methodsAnd 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
aesprimahack, 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 ;)