Skip to content

Commit

Permalink
fix(runtime): emit null for keyed root value
Browse files Browse the repository at this point in the history
this is what jsonpath-plus does
  • Loading branch information
P0lip committed Feb 4, 2024
1 parent c661f20 commit edab477
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,28 @@ describe('Nimma', () => {
});
});

it('$..info^^', () => {
const document = {
info: {},
};

const collected = collect(document, ['$..info^^']);

expect(collected).to.deep.eq({});
});

it('$..info^~', () => {
const document = {
info: {},
};

const collected = collect(document, ['$..info^~']);

expect(collected).to.deep.eq({
'$..info^~': [[null, []]],
});
});

forEach([
Object.preventExtensions({
shirts: Object.seal({
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/codegen-functions/in-bounds.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function (sandbox, pos, start, end, step) {
const value = sandbox.parentAt(-1);
const value = sandbox.valueAt(-1);
const actualStart =
start < 0
? Math.max(0, start + value.length)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/sandbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Sandbox {
return dumpPath(this.#path);
}

parentAt(i) {
valueAt(i) {
return this.#history[this.#path.length + i];
}

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/scope.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export default class Scope {
let path;
let value;
if (pos > 0) {
path = this.path.slice(0, Math.max(0, this.path.length - pos));
value = this.sandbox.parentAt(-pos);
path = this.path.slice(0, this.path.length - pos);
value = this.sandbox.valueAt(-pos);
} else {
path = this.path.slice();
value = this.sandbox.value;
Expand All @@ -129,7 +129,7 @@ export default class Scope {
} else {
fn({
path,
value: path.length === 0 ? void 0 : path[path.length - 1],
value: path.length === 0 ? null : path[path.length - 1],
});
}
}
Expand Down

0 comments on commit edab477

Please sign in to comment.