( whereCallback: (row: SingleRowRefProxy<T>) => any, )
| 187 | * @returns A function that takes an item and returns true if it matches the filter |
| 188 | */ |
| 189 | export function createFilterFunction<T extends object>( |
| 190 | whereCallback: (row: SingleRowRefProxy<T>) => any, |
| 191 | ): (item: T) => boolean { |
| 192 | return (item: T): boolean => { |
| 193 | try { |
| 194 | // First try the RefProxy approach for query builder functions |
| 195 | const singleRowRefProxy = createSingleRowRefProxy<T>() |
| 196 | const whereExpression = whereCallback(singleRowRefProxy) |
| 197 | const expression = toExpression(whereExpression) |
| 198 | const evaluator = compileSingleRowExpression(expression) |
| 199 | const result = evaluator(item as Record<string, unknown>) |
| 200 | // WHERE clauses should always evaluate to boolean predicates (Kevin's feedback) |
| 201 | return toBooleanPredicate(result) |
| 202 | } catch { |
| 203 | // If RefProxy approach fails (e.g., arithmetic operations), fall back to direct evaluation |
| 204 | try { |
| 205 | // Create a simple proxy that returns actual values for arithmetic operations |
| 206 | const simpleProxy = new Proxy(item as any, { |
| 207 | get(target, prop) { |
| 208 | return target[prop] |
| 209 | }, |
| 210 | }) as SingleRowRefProxy<T> |
| 211 | |
| 212 | const result = whereCallback(simpleProxy) |
| 213 | return toBooleanPredicate(result) |
| 214 | } catch { |
| 215 | // If both approaches fail, exclude the item |
| 216 | return false |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Creates a filter function from a pre-compiled expression |
nothing calls this directly
no test coverage detected
searching dependent graphs…