Skip to content

Commit 1b442da

Browse files
committed
- Fix: If resultType is "all", if path resolves internally to a
non-array (string), ensure it is converted to an array before converting to pointer for `pointer` - Testing: Improve coverage
1 parent 90fa96a commit 1b442da

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
- Fix: wrap: false returning inconsistent data types (@CacheControl)
88
- Fix: Ensure throwing with a bad result type
99
- Fix: Avoid erring when value before parent selector is falsey
10+
- Fix: If `resultType` is "all", if path resolves internally to a
11+
non-array (string), ensure it is converted to an array before
12+
converting to pointer for `pointer`
1013
- Enhancement: Allow path as array in non-object signature
1114
- Linting (ESLint): As per latest ash-nazg
1215
- Linting (ESLint): Remove redundant "use strict" with switch to ESM

src/jsonpath.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,16 @@ JSONPath.prototype._getPreferredOutput = function (ea) {
347347
switch (resultType) {
348348
default:
349349
throw new TypeError('Unknown result type');
350-
case 'all':
351-
ea.pointer = JSONPath.toPointer(ea.path);
350+
case 'all': {
351+
const path = Array.isArray(ea.path)
352+
? ea.path
353+
: JSONPath.toPathArray(ea.path);
354+
ea.pointer = JSONPath.toPointer(path);
352355
ea.path = typeof ea.path === 'string'
353356
? ea.path
354357
: JSONPath.toPathString(ea.path);
355358
return ea;
356-
case 'value': case 'parent': case 'parentProperty':
359+
} case 'value': case 'parent': case 'parentProperty':
357360
return ea[resultType];
358361
case 'path':
359362
return JSONPath.toPathString(ea[resultType]);

test/test.callback.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,52 @@ describe('JSONPath - Callback', function () {
5454
jsonpath({json, path: '$.store.bicycle', resultType: 'value', wrap: false, callback});
5555
assert.deepEqual(result, expected);
5656
});
57+
58+
it('Callback with `resultType`: "all"', () => {
59+
const expected = [
60+
'value',
61+
{
62+
path: "$['store']['bicycle']",
63+
value: json.store.bicycle,
64+
parent: json.store,
65+
parentProperty: 'bicycle',
66+
pointer: '/store/bicycle',
67+
hasArrExpr: undefined
68+
},
69+
{
70+
path: "$['store']['bicycle']",
71+
value: json.store.bicycle,
72+
parent: json.store,
73+
parentProperty: 'bicycle',
74+
pointer: '/store/bicycle',
75+
hasArrExpr: undefined
76+
}
77+
];
78+
let result;
79+
/**
80+
*
81+
* @param {PlainObject} data
82+
* @param {string} type
83+
* @param {PlainObject} fullData
84+
* @returns {void}
85+
*/
86+
function callback (data, type, fullData) {
87+
if (!result) {
88+
result = [];
89+
}
90+
result.push(type, data, fullData);
91+
}
92+
jsonpath({json, path: '$.store.bicycle', resultType: 'all', wrap: false, callback});
93+
assert.deepEqual(result[0], expected[0]);
94+
// Todo[chai@>=5]: Error with circular perhaps will be resolved per https://github.com/chaijs/chai/issues/1109#issuecomment-395980733
95+
// so can try directly comparing whole object (as commented out) rather
96+
// than in parts as below that
97+
// assert.deepEqual(result, expected);
98+
Object.keys(result[1]).forEach((prop) => {
99+
assert.deepEqual(result[1][prop], expected[1][prop]);
100+
});
101+
Object.keys(result[2]).forEach((prop) => {
102+
assert.deepEqual(result[2][prop], expected[2][prop]);
103+
});
104+
});
57105
});

0 commit comments

Comments
 (0)