Skip to content

Commit bea4247

Browse files
committed
- Add test case for issue #126
- Docs: Link to XPath 2.0 tester
1 parent 743fd1f commit bea4247

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ and the following XML representation:
301301

302302
Please note that the XPath examples below do not distinguish between
303303
retrieving elements and their text content (except where useful for
304-
comparisons or to prevent ambiguity).
304+
comparisons or to prevent ambiguity). Note: to test the XPath examples
305+
(including 2.0 ones), [this demo](http://videlibri.sourceforge.net/cgi-bin/xidelcgi)
306+
may be helpful (set to `xml` or `xml-strict`).
305307

306308
| XPath | JSONPath | Result | Notes |
307309
| ----------------- | ---------------------- | ------------------------------------- | ----- |

badges/tests-badge.svg

Lines changed: 1 addition & 1 deletion
Loading

test/test.callback.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,56 @@ describe('JSONPath - Callback', function () {
102102
assert.deepEqual(result[2][prop], expected[2][prop]);
103103
});
104104
});
105+
106+
// https://github.com/s3u/JSONPath/issues/126
107+
it('Using callback to set', function () {
108+
const expected = {
109+
age: 30,
110+
email: 'abc@example.com',
111+
'something_deeper': {
112+
abc: 1,
113+
quantity: 11
114+
},
115+
first_name: 'John',
116+
last_name: 'Doe'
117+
};
118+
const givenPerson = {
119+
age: 30,
120+
email: 'abc@example.com',
121+
// let's add first_name, last_name fields
122+
'something_deeper': {
123+
abc: 1
124+
// let's add quantity here
125+
}
126+
};
127+
128+
// defined an object with "json_path":"value" format,
129+
// made sure it is not a deep object.
130+
const obj1 = {
131+
$: {
132+
'first_name': 'John',
133+
'last_name': 'Doe'
134+
},
135+
'$.something_deeper': {
136+
quantity: 11
137+
}
138+
};
139+
140+
// eslint-disable-next-line compat/compat
141+
Object.entries(obj1).forEach(([path, valuesToSet]) => {
142+
jsonpath({
143+
json: givenPerson,
144+
path,
145+
wrap: false,
146+
callback (obj) {
147+
// eslint-disable-next-line compat/compat
148+
Object.entries(valuesToSet).forEach(([key, val]) => {
149+
obj[key] = val;
150+
});
151+
}
152+
});
153+
});
154+
const result = givenPerson;
155+
assert.deepEqual(result, expected);
156+
});
105157
});

0 commit comments

Comments
 (0)