Skip to content

Commit

Permalink
- Add test case for issue JSONPath-Plus#126
Browse files Browse the repository at this point in the history
- Docs: Link to XPath 2.0 tester
  • Loading branch information
brettz9 committed Jun 28, 2020
1 parent 743fd1f commit bea4247
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ and the following XML representation:

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

| XPath | JSONPath | Result | Notes |
| ----------------- | ---------------------- | ------------------------------------- | ----- |
Expand Down
2 changes: 1 addition & 1 deletion badges/tests-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions test/test.callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,56 @@ describe('JSONPath - Callback', function () {
assert.deepEqual(result[2][prop], expected[2][prop]);
});
});

// https://github.com/s3u/JSONPath/issues/126
it('Using callback to set', function () {
const expected = {
age: 30,
email: '[email protected]',
'something_deeper': {
abc: 1,
quantity: 11
},
first_name: 'John',
last_name: 'Doe'
};
const givenPerson = {
age: 30,
email: '[email protected]',
// let's add first_name, last_name fields
'something_deeper': {
abc: 1
// let's add quantity here
}
};

// defined an object with "json_path":"value" format,
// made sure it is not a deep object.
const obj1 = {
$: {
'first_name': 'John',
'last_name': 'Doe'
},
'$.something_deeper': {
quantity: 11
}
};

// eslint-disable-next-line compat/compat
Object.entries(obj1).forEach(([path, valuesToSet]) => {
jsonpath({
json: givenPerson,
path,
wrap: false,
callback (obj) {
// eslint-disable-next-line compat/compat
Object.entries(valuesToSet).forEach(([key, val]) => {
obj[key] = val;
});
}
});
});
const result = givenPerson;
assert.deepEqual(result, expected);
});
});

0 comments on commit bea4247

Please sign in to comment.