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

Function canOptimizeSimpleComparison

packages/db/src/utils/index-optimization.ts:579–605  ·  view source on GitHub ↗

* Checks if a simple comparison can be optimized

(expression: BasicExpression, collection: CollectionLike<T, TKey>)

Source from the content-addressed store, hash-verified

577 * Checks if a simple comparison can be optimized
578 */
579function canOptimizeSimpleComparison<
580 T extends object,
581 TKey extends string | number,
582>(expression: BasicExpression, collection: CollectionLike<T, TKey>): boolean {
583 if (expression.type !== `func` || expression.args.length !== 2) {
584 return false
585 }
586
587 const leftArg = expression.args[0]!
588 const rightArg = expression.args[1]!
589
590 // Check both directions: field op value AND value op field
591 let fieldPath: Array<string> | null = null
592
593 if (leftArg.type === `ref` && rightArg.type === `val`) {
594 fieldPath = (leftArg as any).path
595 } else if (leftArg.type === `val` && rightArg.type === `ref`) {
596 fieldPath = (rightArg as any).path
597 }
598
599 if (fieldPath) {
600 const index = findIndexForField(collection, fieldPath)
601 return index !== undefined
602 }
603
604 return false
605}
606
607/**
608 * Optimizes AND expressions

Callers 1

canOptimizeExpressionFunction · 0.85

Calls 1

findIndexForFieldFunction · 0.85

Tested by

no test coverage detected