-
Notifications
You must be signed in to change notification settings - Fork 215
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
.apply
skips the last path
#183
Comments
can confirm. about to write the same bug report. |
might have something to do with the use of Line 42 in c1dd8ec
|
This hot fix seems to work. Need reviews though, I just learn nodejs 2 weeks ago. var assert = require('assert');
var jp = require('jsonpath');
jp.apply = function(obj, string, fn) {
assert.ok(obj instanceof Object, "obj needs to be an object");
assert.ok(string, "we need a path");
assert.equal(typeof fn, "function", "fn needs to be function")
var nodes = this.nodes(obj, string).sort(function(a, b) {
// sort nodes so we apply from the bottom up
return b.path.length - a.path.length;
});
nodes.forEach(function(node) {
var key = node.path.slice(-1);
var parent = this.value(obj, this.stringify(node.path.slice(0, -1)));
var val = node.value = fn.call(obj, parent[key]);
parent[key] = val;
}, this);
return nodes;
} |
you can update the tests https://github.com/dchester/jsonpath/blob/master/test/query.js to ensure it works? |
Sure, but after looking around, I think https://github.com/dchester/jsonpath/blob/master/test/sugar.js might be a better place. The rationale is would you agree? |
One more thing @PsyGik, I just noticed that the expected result in your opening post does not match the behaviour of Specifically, the document said I'm going to stick with the README for now. |
A test describing expected behaviour has also been added.
Yeah you are right. The values should have been formatted to uppercase. I was focusing on the main issue i.e missing the last key from the paths array |
given the code below:
Expected:
Actual:
Runkit example: https://runkit.com/psygik/6538cdcc485c4f000831f0dc
The text was updated successfully, but these errors were encountered: