From 5b1b1cdaaa1daaba660063fde0615571fa591f6e Mon Sep 17 00:00:00 2001 From: Kat Huang Date: Tue, 9 Jan 2024 16:28:48 -0700 Subject: [PATCH] WIP --- graphql/filter.ts | 31 +++++++++++++++++++++++++++++++ test/graphql/filter.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 graphql/filter.ts create mode 100644 test/graphql/filter.test.ts diff --git a/graphql/filter.ts b/graphql/filter.ts new file mode 100644 index 0000000..41f74e8 --- /dev/null +++ b/graphql/filter.ts @@ -0,0 +1,31 @@ +type BaseFilter = { + createVars: () => { [key: string]: any }; +}; + +function createBaseFilter(key: string, content: any): BaseFilter { + return { + createVars: () => ({ [key]: content }), + }; +} + +export function createAndFilter(filters: Array): BaseFilter { + const filterVars = filters.map((filter) => filter.createVars()); + return createBaseFilter("andFilters", { filters: filterVars }); +} + +export function createCategoryFilter( + feature: string, + value: string, +): BaseFilter { + const content = { [feature]: { value } }; + return createBaseFilter(feature, content); +} + +export function createRangeFilter( + feature: string, + greaterThanEqualTo: number, + lessThan: number, +): BaseFilter { + const content = { greaterThanEqualTo, lessThan }; + return createBaseFilter(feature, content); +} diff --git a/test/graphql/filter.test.ts b/test/graphql/filter.test.ts new file mode 100644 index 0000000..ca79b99 --- /dev/null +++ b/test/graphql/filter.test.ts @@ -0,0 +1,28 @@ +import { createAndFilter, createRangeFilter } from "../../graphql/filter"; + +describe("create filterInput", () => { + it("should create a filter input with the correct structure", () => { + const feature = "cueObjectAngle"; + const greaterThanEqualTo = 10; + const lessThan = 20; + + const rangeFilter = createRangeFilter( + feature, + greaterThanEqualTo, + lessThan, + ); + const andFilterInput = createAndFilter([rangeFilter]); + expect(andFilterInput.createVars()).toEqual({ + andFilters: { + filters: [ + { + [feature]: { + greaterThanEqualTo, + lessThan, + }, + }, + ], + }, + }); + }); +});