Skip to content

Commit

Permalink
Fixed #70
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest committed Feb 3, 2025
1 parent 2166204 commit 02114dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/rules/isIn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
export default (value: any, options: any[] | string): boolean => {
const list: any[] = Array.isArray(options) ? options : options.split(",");
export default (value: any, ...args: any[]): boolean => {
// Let's check the last item is IContext
const lastItem: any = args.at(-1);
if (lastItem.definition && lastItem.field) {
args.pop();
}

const [options] = args;

const list: any[] = Array.isArray(options)
? options
: (options as string).split(",");
const listAsString = list.map((item) => String(item).trim());

if (Array.isArray(value)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/rules/in.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,18 @@ describe("isIn() ", () => {

it("should be able to parse string values", async () => {
expect(isIn("A", "A,B,B")).toBe(true);
expect(isIn("B", "A,B,C")).toBe(true);
expect(isIn("C", "A,B,C")).toBe(true);
expect(isIn("D", "A,B,C")).toBe(false);

expect(isIn("a", ["a", "b", "c"])).toBe(true);
expect(isIn("b", ["a", "b", "c"])).toBe(true);
expect(isIn("c", ["a", "b", "c"])).toBe(true);
expect(isIn("d", ["a", "b", "c"])).toBe(false);
});

it("should be able to work with spread-string options", async () => {
expect(isIn("A", ...["A,B,B"])).toBe(true);
expect(isIn("A", ...["A", "B", "C"])).toBe(true);
});
});

0 comments on commit 02114dc

Please sign in to comment.