Skip to content

Commit b7673ca

Browse files
committed
- Fix: Allow empty string keys
- Testing: Improve coverage
1 parent 1b442da commit b7673ca

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix: Require `json` as "own" property
77
- Fix: wrap: false returning inconsistent data types (@CacheControl)
88
- Fix: Ensure throwing with a bad result type
9+
- Fix: Allow empty string keys
910
- Fix: Avoid erring when value before parent selector is falsey
1011
- Fix: If `resultType` is "all", if path resolves internally to a
1112
non-array (string), ensure it is converted to an array before

src/jsonpath.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,9 @@ JSONPath.prototype._trace = function (
594594
));
595595
}
596596
// simple case--directly follow property
597-
} else if (!literalPriority && val && hasOwnProp.call(val, loc)) {
597+
} else if (
598+
!literalPriority && (val || val === '') && hasOwnProp.call(val, loc)
599+
) {
598600
addRet(
599601
this._trace(x, val[loc], push(path, loc), val, loc, callback,
600602
hasArrExpr, true)

test/test.parent-selector.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,28 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
3535
assert.deepEqual(result, expected);
3636
});
3737

38+
// Todo: Handle "this._trace(...).filter is not a function" error`
3839
/*
39-
// Todo: Handle Maximum call stack error getting here
40-
it('parent root', () => {
41-
const jsonSimple = {};
42-
const expected = json.children;
43-
const result = jsonpath({json: jsonSimple, path: '^', flatten: true});
40+
it.only('parent root', () => {
41+
const jsonSimple = {
42+
children: null
43+
};
44+
const expected = jsonSimple.children;
45+
const result = jsonpath({json: jsonSimple, path: '$^', flatten: true});
46+
console.log('result', result);
4447
assert.deepEqual(result, expected);
4548
});
4649
*/
4750

51+
it('empty string key (parent of)', () => {
52+
const jsonSimple = {
53+
'': null
54+
};
55+
const expected = jsonSimple;
56+
const result = jsonpath({json: jsonSimple, path: '^', wrap: false});
57+
assert.deepEqual(result, expected);
58+
});
59+
4860
it('no such parent', () => {
4961
const result = jsonpath({json, path: 'name^^'});
5062
assert.deepEqual(result, []);

0 commit comments

Comments
 (0)