|
1 | | -import { |
2 | | - IdSearchOptions, |
3 | | - NumberArraySearchOptions, |
4 | | - NumberSearchOptions, |
5 | | - StringArraySearchOptions, |
6 | | - StringSearchOptions |
7 | | -} from "../inputs/searchOptions.js"; |
8 | | - |
9 | 1 | type OperandType = string | number | bigint | string[] | bigint[]; |
10 | | -type OperatorType = "eq" | "gt" | "gte" | "lt" | "lte" | "ilike" | "contains" | "startsWith" | "endsWith" |
11 | | - |
12 | | -export const generateFilterValues = (column: string, operator: OperatorType, operand: OperandType) => { |
13 | | - console.log("generateFilterValues", column, operator, operand); |
| 2 | +type OperatorType = |
| 3 | + | "eq" |
| 4 | + | "gt" |
| 5 | + | "gte" |
| 6 | + | "lt" |
| 7 | + | "lte" |
| 8 | + | "ilike" |
| 9 | + | "contains" |
| 10 | + | "startsWith" |
| 11 | + | "endsWith"; |
| 12 | + |
| 13 | +enum OperatorSymbols { |
| 14 | + eq = "=", |
| 15 | + gt = ">", |
| 16 | + gte = ">=", |
| 17 | + lt = "<", |
| 18 | + lte = "<=", |
| 19 | + ilike = "ilike", |
| 20 | +} |
14 | 21 |
|
| 22 | +export const generateFilterValues = ( |
| 23 | + column: string, |
| 24 | + operator: OperatorType, |
| 25 | + operand: OperandType, |
| 26 | +) => { |
15 | 27 | switch (operator) { |
16 | 28 | case "eq": |
17 | | - return [column, "=", operand]; |
| 29 | + return [column, OperatorSymbols.eq, operand]; |
18 | 30 | case "gt": |
19 | | - return [column, ">", operand]; |
| 31 | + return [column, OperatorSymbols.gt, operand]; |
20 | 32 | case "gte": |
21 | | - return [column, ">=", operand]; |
| 33 | + return [column, OperatorSymbols.gte, operand]; |
22 | 34 | case "lt": |
23 | | - return [column, "<", operand]; |
| 35 | + return [column, OperatorSymbols.lt, operand]; |
24 | 36 | case "lte": |
25 | | - return [column, "<=", operand]; |
| 37 | + return [column, OperatorSymbols.lte, operand]; |
26 | 38 | case "contains": |
27 | | - return [column, "like", `%${operand}%`]; |
| 39 | + return [column, OperatorSymbols.ilike, operand]; |
28 | 40 | case "startsWith": |
29 | | - return [column, "like", `${operand}%`]; |
| 41 | + return [column, OperatorSymbols.ilike, operand]; |
30 | 42 | case "endsWith": |
31 | | - return [column, "like", `%${operand}`]; |
32 | | - } |
33 | | - |
34 | | - return []; |
35 | | -}; |
36 | | - |
37 | | - |
38 | | - |
39 | | -const generateArrayFilters = (value: NumberArraySearchOptions | StringArraySearchOptions, column: string) => { |
40 | | - const filters: [OperatorType, string, OperandType][] = []; |
41 | | - for (const [operator, operand] of Object.entries(value)) { |
42 | | - if (!operand) continue; |
43 | | - |
44 | | - // Assert operand is an array of numbers |
45 | | - if (!Array.isArray(operand)) { |
46 | | - throw new Error(`Expected operand to be an array, but got ${typeof operand}`); |
47 | | - } |
48 | | - |
49 | | - switch (operator) { |
50 | | - case "contains": |
51 | | - filters.push(["contains", column, operand]); |
52 | | - break; |
53 | | - } |
54 | | - } |
55 | | - return filters; |
56 | | -}; |
57 | | - |
58 | | -function isStringSearchOptions(value: unknown): value is StringSearchOptions { |
59 | | - if (typeof value !== "object" || value === null) { |
60 | | - return false; |
61 | | - } |
62 | | - |
63 | | - const possibleStringSearchOptions = value as Partial<StringSearchOptions>; |
64 | | - |
65 | | - // Check for properties unique to StringSearchOptions |
66 | | - const keys = ["eq", "contains", "startsWith", "endsWith"]; |
67 | | - return keys.some(key => key in possibleStringSearchOptions); |
68 | | -} |
69 | | - |
70 | | -function isNumberSearchOptions(value: unknown): value is NumberSearchOptions { |
71 | | - if (typeof value !== "object" || value === null) { |
72 | | - return false; |
73 | | - } |
74 | | - |
75 | | - const possibleNumberSearchOptions = value as Partial<NumberSearchOptions>; |
76 | | - |
77 | | - // Check for properties unique to NumberSearchOptions |
78 | | - const keys = ["eq", "gt", "gte", "lt", "lte"]; |
79 | | - return keys.some(key => key in possibleNumberSearchOptions); |
80 | | -} |
81 | | - |
82 | | -function isIdSearchOptions(value: unknown): value is IdSearchOptions { |
83 | | - if (typeof value !== "object" || value === null) { |
84 | | - return false; |
85 | | - } |
86 | | - |
87 | | - const possibleIdSearchOptions = value as Partial<NumberSearchOptions>; |
88 | | - |
89 | | - // Check for properties unique to IdSearchOptions |
90 | | - const keys = ["eq", "contains", "startsWith", "endsWith"]; |
91 | | - return keys.some(key => key in possibleIdSearchOptions); |
92 | | - |
93 | | -} |
94 | | - |
95 | | -function isStringArraySearchOptions(value: unknown): value is StringArraySearchOptions { |
96 | | - if (!Array.isArray(value) || value === null) { |
97 | | - return false; |
98 | | - } |
99 | | - |
100 | | - const possibleStringArraySearchOptions = value as Partial<StringArraySearchOptions>; |
101 | | - |
102 | | - // Check for properties unique to StringArraySearchOptions |
103 | | - const keys = ["contains"]; |
104 | | - return keys.some(key => key in possibleStringArraySearchOptions); |
105 | | -} |
106 | | - |
107 | | -function isNumberArraySearchOptions(value: unknown): value is NumberArraySearchOptions { |
108 | | - if (!Array.isArray(value) || value === null) { |
109 | | - return false; |
110 | | - } |
111 | | - |
112 | | - const possibleNumberArraySearchOptions = value as Partial<NumberArraySearchOptions>; |
113 | | - |
114 | | - // Check for properties unique to NumberArraySearchOptions |
115 | | - const keys = ["contains"]; |
116 | | - return keys.some(key => key in possibleNumberArraySearchOptions); |
117 | | -} |
118 | | - |
119 | | -const buildFilters = (value: unknown, column: string) => { |
120 | | - |
121 | | - if (isStringArraySearchOptions(value) || isNumberArraySearchOptions(value)) { |
122 | | - return generateArrayFilters(value, column); |
| 43 | + return [column, OperatorSymbols.ilike, operand]; |
123 | 44 | } |
124 | 45 |
|
125 | 46 | return []; |
126 | 47 | }; |
127 | | - |
128 | | - |
0 commit comments