MCPcopy Create free account
hub / github.com/effect-app/libs / computeProjectionValue

Function computeProjectionValue

packages/infra/src/Store/Memory.ts:65–170  ·  view source on GitHub ↗
(
  row: FieldValues,
  computed: ComputedProjectionIrExpression
)

Source from the content-addressed store, hash-verified

63}
64
65const computeProjectionValue = (
66 row: FieldValues,
67 computed: ComputedProjectionIrExpression
68) => {
69 const relation = get(row, computed.path)
70 if (!Array.isArray(relation)) {
71 return emptyValueFor(computed._tag)
72 }
73 if (computed._tag === "relation-length") {
74 return relation.length
75 }
76 const filter = stripRelationFilterPaths(computed.filter, computed.path)
77 // empty filter = unconditional match (codeFilter3_ uses eval on a built
78 // string and chokes on `( )`, so short-circuit before invoking it).
79 const matches = filter.length === 0
80 ? (_value: unknown) => true
81 : (value: unknown) => codeFilter3_(filter, value)
82 const evalExpr = (value: unknown, expression: ComputedProjectionMathIrExpression): number => {
83 switch (expression._tag) {
84 case "field": {
85 const v = get(value, expression.field)
86 return typeof v === "number" ? v : Number(v) || 0
87 }
88 case "mul":
89 return evalExpr(value, expression.left) * evalExpr(value, expression.right)
90 default:
91 return assertUnreachable(expression)
92 }
93 }
94 switch (computed._tag) {
95 case "relation-count":
96 return relation.reduce<number>((acc, value) => matches(value) ? acc + 1 : acc, 0)
97 case "relation-any":
98 return relation.some(matches)
99 case "relation-every":
100 return relation.every(matches)
101 case "relation-distinct-count": {
102 const seen = new Set<unknown>()
103 for (const value of relation) {
104 if (matches(value)) seen.add(get(value, computed.field))
105 }
106 return seen.size
107 }
108 case "relation-sum":
109 return relation.reduce<number>((acc, value) => {
110 if (!matches(value)) return acc
111 const v = get(value, computed.field)
112 return acc + (typeof v === "number" ? v : Number(v) || 0)
113 }, 0)
114 case "relation-sum-expr":
115 return relation.reduce<number>((acc, value) => {
116 if (!matches(value)) return acc
117 return acc + evalExpr(value, computed.expression)
118 }, 0)
119 case "relation-sum-expr-by": {
120 const totals = new Map<unknown, number>()
121 for (const value of relation) {
122 if (!matches(value)) continue

Callers 1

selectPerRowFunction · 0.85

Calls 15

getFunction · 0.90
codeFilter3_Function · 0.90
assertUnreachableFunction · 0.90
emptyValueForFunction · 0.85
stripRelationFilterPathsFunction · 0.85
matchesFunction · 0.85
evalExprFunction · 0.85
someMethod · 0.80
pushMethod · 0.80
addMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…