Skip to content

Commit bc6bca7

Browse files
committed
- Fix: Avoid erring when value before parent selector is falsey
1 parent 19cb462 commit bc6bca7

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix: wrap: false returning inconsistent data types (@CacheControl)
66
- Fix: Ensure throwing with a bad result type
7+
- Fix: Avoid erring when value before parent selector is falsey
78
- Enhancement: Allow path as array in non-object signature
89
- Linting (ESLint): As per latest ash-nazg
910
- Linting (ESLint): Remove redundant "use strict" with switch to ESM

src/jsonpath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ JSONPath.prototype._trace = function (
604604
if (this._hasParentSelector) {
605605
for (let t = 0; t < ret.length; t++) {
606606
const rett = ret[t];
607-
if (rett.isParentSelector) {
607+
if (rett && rett.isParentSelector) {
608608
const tmp = that._trace(
609609
rett.expr, val, rett.path, parent, parentPropName, callback,
610610
hasArrExpr

test/test.parent-selector.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,38 @@ describe('JSONPath - Parent selector', function () {
3636
const result = jsonpath({json, path: 'name^^'});
3737
assert.deepEqual([], result);
3838
});
39+
40+
it('select sibling via parent (with non-match present)', () => {
41+
const jsonMultipleChildren = {
42+
"name": "root",
43+
"children": [
44+
{"name": "child1", "children": [{"name": "child1_1"}, {"name": "child1_2"}]},
45+
{"name": "child2", "children": [{"name": "child2_1"}]},
46+
{"name": "child3", "children": [{"name": "child3_1"}, {"name": "child3_2"}]},
47+
{"name": "child4", "children": [{"name": "child4_1"}, {"name": "child3_1"}]}
48+
]
49+
};
50+
const expected = [{"name": "child3_2"}];
51+
const result = jsonpath({
52+
json: jsonMultipleChildren,
53+
path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'
54+
});
55+
assert.deepEqual(expected, result);
56+
});
57+
it('select sibling via parent (with multiple results)', () => {
58+
const jsonMultipleChildren = {
59+
"name": "root",
60+
"children": [
61+
{"name": "child1", "children": [{"name": "child1_1"}, {"name": "child1_2"}]},
62+
{"name": "child2", "children": [{"name": "child2_1"}]},
63+
{"name": "child3", "children": [{"name": "child3_1"}, {"name": "child3_2"}, {"name": "child3_2", second: true}]}
64+
]
65+
};
66+
const expected = [{"name": "child3_2"}, {"name": "child3_2", second: true}];
67+
const result = jsonpath({
68+
json: jsonMultipleChildren,
69+
path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'
70+
});
71+
assert.deepEqual(expected, result);
72+
});
3973
});

0 commit comments

Comments
 (0)