Skip to content

Commit be5a2b5

Browse files
authored
fix: IN operator can be a list or single value (#327)
1 parent 7a466c6 commit be5a2b5

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

.changeset/yellow-rabbits-marry.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 to work correctly with array values in predicate parser

src/lib/predicateParser.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,14 @@ describe("Predicate filter", () => {
369369
),
370370
).toBeFalsy();
371371
});
372+
373+
test("in operator with array values", async () => {
374+
expect(match(`arrayProperty in ("foo")`)).toBeTruthy();
375+
expect(match(`arrayProperty in ("bar")`)).toBeTruthy();
376+
expect(match(`arrayProperty in ("missing")`)).toBeFalsy();
377+
expect(match(`arrayProperty in ("foo", "bar")`)).toBeTruthy();
378+
expect(match(`arrayProperty in ("missing", "alsomissing")`)).toBeFalsy();
379+
});
372380
});
373381

374382
describe("Report parse errors", () => {

src/lib/predicateParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ const generateMatchFunc = (predicate: string): MatchFunc => {
403403
resolveSymbol(item, vars),
404404
);
405405
const value = resolveValue(obj, left);
406+
407+
if (Array.isArray(value)) {
408+
return inValues.some((inValue: any) => value.includes(inValue));
409+
}
410+
406411
return inValues.includes(value);
407412
};
408413
})

0 commit comments

Comments
 (0)