Skip to content

Commit

Permalink
fix: bug with paths containing array position
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 7, 2025
1 parent e613795 commit b6c399d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/node/test/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe("selectors", () => {
paths: ["$.a.c.1"],
expected: [4],
},
{
value: '{ "a": [ {"b": 1}, {"c": 2} ] }',
paths: ["$.a.0.b"],
expected: [1],
},
];

testData.forEach(({ value, paths, expected }) => {
Expand All @@ -53,6 +58,8 @@ describe("selectors", () => {
i += 1;
},
);

expect(i).toEqual(expected.length);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/plainjs/dist/deno/tokenparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class TokenParser {
const selector = path[i];
const key = this.stack[i + 1].key;
if (selector === "*") continue;
if (selector !== key) return false;
if (selector !== key?.toString()) return false;
}

const selector = path[path.length - 1];
Expand Down
2 changes: 1 addition & 1 deletion packages/plainjs/src/tokenparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class TokenParser {
const selector = path[i];
const key = this.stack[i + 1].key;
if (selector === "*") continue;
if (selector !== key) return false;
if (selector !== key?.toString()) return false;
}

const selector = path[path.length - 1];
Expand Down
7 changes: 7 additions & 0 deletions packages/plainjs/test/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe("selectors", () => {
paths: ["$.a.c.1"],
expected: [4],
},
{
value: '{ "a": [ {"b": 1}, {"c": 2} ] }',
paths: ["$.a.0.b"],
expected: [1],
},
];

testData.forEach(({ value, paths, expected }) => {
Expand All @@ -53,6 +58,8 @@ describe("selectors", () => {
i += 1;
},
);

expect(i).toEqual(expected.length);
});
});

Expand Down
7 changes: 7 additions & 0 deletions packages/whatwg/test/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe("selectors", () => {
paths: ["$.a.c.1"],
expected: [4],
},
{
value: '{ "a": [ {"b": 1}, {"c": 2} ] }',
paths: ["$.a.0.b"],
expected: [1],
},
];

testData.forEach(({ value, paths, expected }) => {
Expand All @@ -53,6 +58,8 @@ describe("selectors", () => {
i += 1;
},
);

expect(i).toEqual(expected.length);
});
});

Expand Down

0 comments on commit b6c399d

Please sign in to comment.