MCPcopy Create free account
hub / github.com/TanStack/db / combineConditions

Function combineConditions

packages/db/src/query/predicate-utils.ts:999–1029  ·  view source on GitHub ↗

* Combine multiple conditions into a single predicate using AND logic. * Flattens nested AND operations to avoid unnecessary nesting.

(
  conditions: Array<BasicExpression<boolean>>,
)

Source from the content-addressed store, hash-verified

997 * Flattens nested AND operations to avoid unnecessary nesting.
998 */
999function combineConditions(
1000 conditions: Array<BasicExpression<boolean>>,
1001): BasicExpression<boolean> {
1002 if (conditions.length === 0) {
1003 return { type: `val`, value: true } as BasicExpression<boolean>
1004 } else if (conditions.length === 1) {
1005 return conditions[0]!
1006 } else {
1007 // Flatten all conditions, including those that are already AND operations
1008 const flattenedConditions: Array<BasicExpression<boolean>> = []
1009
1010 for (const condition of conditions) {
1011 if (condition.type === `func` && condition.name === `and`) {
1012 // Flatten nested AND operations
1013 flattenedConditions.push(...condition.args)
1014 } else {
1015 flattenedConditions.push(condition)
1016 }
1017 }
1018
1019 if (flattenedConditions.length === 1) {
1020 return flattenedConditions[0]!
1021 } else {
1022 return {
1023 type: `func`,
1024 name: `and`,
1025 args: flattenedConditions,
1026 } as BasicExpression<boolean>
1027 }
1028 }
1029}
1030
1031/**
1032 * Find a predicate with a specific operator and value

Callers 1

minusWherePredicatesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected