MCPcopy Index your code
hub / github.com/TanStack/db / extractSimpleComparisons

Function extractSimpleComparisons

packages/db/src/query/expression-helpers.ts:327–474  ·  view source on GitHub ↗
(
  expr: BasicExpression<boolean> | undefined | null,
)

Source from the content-addressed store, hash-verified

325 * ```
326 */
327export function extractSimpleComparisons(
328 expr: BasicExpression<boolean> | undefined | null,
329): Array<SimpleComparison> {
330 if (!expr) return []
331
332 const comparisons: Array<SimpleComparison> = []
333
334 function extract(e: BasicExpression): void {
335 if (e.type === `func`) {
336 // Handle AND - recurse into both sides
337 if (e.name === `and`) {
338 e.args.forEach((arg: BasicExpression) => extract(arg))
339 return
340 }
341
342 // Handle NOT - recurse into argument and prefix operator with 'not_'
343 if (e.name === `not`) {
344 const [arg] = e.args
345 if (!arg || arg.type !== `func`) {
346 throw new Error(
347 `extractSimpleComparisons requires a comparison or null check inside 'not' operator.`,
348 )
349 }
350
351 // Handle NOT with null/undefined checks
352 const nullCheckOps = [`isNull`, `isUndefined`]
353 if (nullCheckOps.includes(arg.name)) {
354 const [fieldArg] = arg.args
355 const field = fieldArg?.type === `ref` ? fieldArg.path : null
356
357 if (field) {
358 comparisons.push({
359 field,
360 operator: `not_${arg.name}`,
361 // No value for null/undefined checks
362 })
363 } else {
364 throw new Error(
365 `extractSimpleComparisons requires a field reference for '${arg.name}' operator.`,
366 )
367 }
368 return
369 }
370
371 // Handle NOT with comparison operators
372 const comparisonOps = [`eq`, `gt`, `gte`, `lt`, `lte`, `in`]
373 if (comparisonOps.includes(arg.name)) {
374 const [leftArg, rightArg] = arg.args
375 const field = leftArg?.type === `ref` ? leftArg.path : null
376 const value = rightArg?.type === `val` ? rightArg.value : null
377
378 if (field && value !== undefined) {
379 comparisons.push({
380 field,
381 operator: `not_${arg.name}`,
382 value,
383 })
384 } else {

Calls 1

extractFunction · 0.85

Tested by

no test coverage detected