Skip to content

Commit edab477

Browse files
committed
fix(runtime): emit null for keyed root value
this is what jsonpath-plus does
1 parent c661f20 commit edab477

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/__tests__/index.test.mjs

+22
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,28 @@ describe('Nimma', () => {
14301430
});
14311431
});
14321432

1433+
it('$..info^^', () => {
1434+
const document = {
1435+
info: {},
1436+
};
1437+
1438+
const collected = collect(document, ['$..info^^']);
1439+
1440+
expect(collected).to.deep.eq({});
1441+
});
1442+
1443+
it('$..info^~', () => {
1444+
const document = {
1445+
info: {},
1446+
};
1447+
1448+
const collected = collect(document, ['$..info^~']);
1449+
1450+
expect(collected).to.deep.eq({
1451+
'$..info^~': [[null, []]],
1452+
});
1453+
});
1454+
14331455
forEach([
14341456
Object.preventExtensions({
14351457
shirts: Object.seal({

src/runtime/codegen-functions/in-bounds.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function (sandbox, pos, start, end, step) {
2-
const value = sandbox.parentAt(-1);
2+
const value = sandbox.valueAt(-1);
33
const actualStart =
44
start < 0
55
? Math.max(0, start + value.length)

src/runtime/sandbox.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class Sandbox {
2222
return dumpPath(this.#path);
2323
}
2424

25-
parentAt(i) {
25+
valueAt(i) {
2626
return this.#history[this.#path.length + i];
2727
}
2828

src/runtime/scope.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export default class Scope {
117117
let path;
118118
let value;
119119
if (pos > 0) {
120-
path = this.path.slice(0, Math.max(0, this.path.length - pos));
121-
value = this.sandbox.parentAt(-pos);
120+
path = this.path.slice(0, this.path.length - pos);
121+
value = this.sandbox.valueAt(-pos);
122122
} else {
123123
path = this.path.slice();
124124
value = this.sandbox.value;
@@ -129,7 +129,7 @@ export default class Scope {
129129
} else {
130130
fn({
131131
path,
132-
value: path.length === 0 ? void 0 : path[path.length - 1],
132+
value: path.length === 0 ? null : path[path.length - 1],
133133
});
134134
}
135135
}

0 commit comments

Comments
 (0)