| 68 | } |
| 69 | |
| 70 | function expr(exp1: string, op: Operator, exp2: string): Filter<Document> { |
| 71 | switch (op) { |
| 72 | case "=": |
| 73 | case "is": |
| 74 | return { $eq: [exp1, exp2] }; |
| 75 | case "!=": |
| 76 | case "is not": |
| 77 | return { $ne: [exp1, exp2] }; |
| 78 | case ">": |
| 79 | return { $gt: [exp1, exp2] }; |
| 80 | case ">=": |
| 81 | return { $gte: [exp1, exp2] }; |
| 82 | case "<": |
| 83 | return { $lt: [exp1, exp2] }; |
| 84 | case "<=": |
| 85 | return { $lte: [exp1, exp2] }; |
| 86 | case "in": |
| 87 | return { $in: [exp1, exp2] }; |
| 88 | case "not in": |
| 89 | return { $nin: [exp1, exp2] }; |
| 90 | case "starts with": |
| 91 | return { |
| 92 | $regexMatch: { |
| 93 | input: exp1, |
| 94 | regex: `^${exp2}`, |
| 95 | options: "i", |
| 96 | }, |
| 97 | }; |
| 98 | case "not starts with": |
| 99 | return { |
| 100 | $not: [expr(exp1, "starts with", exp2)], |
| 101 | }; |
| 102 | case "contains": |
| 103 | return { |
| 104 | $regexMatch: { |
| 105 | input: exp1, |
| 106 | regex: exp2, |
| 107 | options: "i", |
| 108 | }, |
| 109 | }; |
| 110 | case "not contains": |
| 111 | return { |
| 112 | $not: [expr(exp1, "contains", exp2)], |
| 113 | }; |
| 114 | case "ends with": |
| 115 | return { |
| 116 | $regexMatch: { |
| 117 | input: exp1, |
| 118 | regex: `${exp2}$`, |
| 119 | options: "i", |
| 120 | }, |
| 121 | }; |
| 122 | case "not ends with": |
| 123 | return { |
| 124 | $not: [expr(exp1, "ends with", exp2)], |
| 125 | }; |
| 126 | default: |
| 127 | throw new Error(`Unsupported operator: ${op}`); |