Skip to content

Commit 7a466c6

Browse files
authored
fix: IN operator can be both a list and a single value (#320)
1 parent 4399311 commit 7a466c6

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

.changeset/plain-bugs-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": patch
3+
---
4+
5+
fix: IN operator can be both a list and a single value

src/lib/predicateParser.test.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -151,43 +151,11 @@ describe("Predicate filter", () => {
151151
expect(match("numberProperty in (1234)")).toBeTruthy();
152152
});
153153

154-
test("in operator with and clause", async () => {
155-
expect(
156-
match("numberProperty in (1234) and stringProperty=:val", {
157-
val: "foobar",
158-
}),
159-
).toBeTruthy();
160-
expect(
161-
match("numberProperty in (1234) and stringProperty=:val", {
162-
val: "other",
163-
}),
164-
).toBeFalsy();
165-
expect(
166-
match("numberProperty in (1235) and stringProperty=:val", {
167-
val: "foobar",
168-
}),
169-
).toBeFalsy();
170-
});
171-
172-
test("within operator with and clause", async () => {
173-
expect(
174-
match(
175-
"geoLocation within circle(5.121310867198959, 52.09068804569714, 2500) and stringProperty=:val",
176-
{ val: "foobar" },
177-
),
178-
).toBeTruthy();
179-
expect(
180-
match(
181-
"geoLocation within circle(5.121310867198959, 52.09068804569714, 2500) and stringProperty=:val",
182-
{ val: "other" },
183-
),
184-
).toBeFalsy();
185-
expect(
186-
match(
187-
"geoLocation within circle(5.121310867198959, 52.09068804569714, 1000) and stringProperty=:val",
188-
{ val: "foobar" },
189-
),
190-
).toBeFalsy();
154+
test("in operator works with with and without parentheses", async () => {
155+
expect(match("numberProperty in :val", { val: 1234 })).toBeTruthy();
156+
expect(match("numberProperty in :val", { val: 1235 })).toBeFalsy();
157+
expect(match("numberProperty in :val", { val: [1234] })).toBeTruthy();
158+
expect(match("numberProperty in :val", { val: [1235] })).toBeFalsy();
191159
});
192160

193161
test("arrayProperty contains all (...)", async () => {

src/lib/predicateParser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,14 @@ const generateMatchFunc = (predicate: string): MatchFunc => {
381381
}
382382
})
383383
.led("IN", 20, ({ left, bp }) => {
384+
const firstToken = lexer.peek();
384385
const expr = parser.parse({ terminals: [bp - 1] });
385-
lexer.expect(")");
386+
387+
// IN can be a single value or a list of values
388+
if (firstToken.match === "(") {
389+
lexer.expect(")");
390+
}
391+
386392
return (obj: any, vars: object) => {
387393
let symbols = expr;
388394
if (!Array.isArray(symbols)) {

0 commit comments

Comments
 (0)