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

Function memFilter

packages/infra/src/Store/Memory.ts:209–311  ·  view source on GitHub ↗
(f: FilterArgs<T, U>)

Source from the content-addressed store, hash-verified

207}
208
209export function memFilter<T extends FieldValues, U extends keyof T = never>(f: FilterArgs<T, U>) {
210 type M = U extends undefined ? T : Pick<T, U>
211 return ((c: T[]): M[] => {
212 const sel = f.select
213
214 const selectPerRow = (r: T[]): M[] => {
215 if (!sel) return r as M[]
216
217 // Detect aggregate mode: any select item has `aggregate` key
218 const hasAggregates = sel.some((s) => typeof s === "object" && s !== null && "aggregate" in s)
219 if (hasAggregates) {
220 // GROUP BY + aggregate
221 const fieldItems = sel.filter((s): s is { key: string; path: string } =>
222 typeof s === "object" && s !== null && "path" in s && !("aggregate" in s)
223 )
224 const aggregateItems = sel.filter((s): s is { key: string; aggregate: AggregateIrExpression } =>
225 typeof s === "object" && s !== null && "aggregate" in s
226 )
227 const groups = new Map<string, T[]>()
228 for (const row of r) {
229 const key = fieldItems.map((fi) => JSON.stringify(get(row, fi.path))).join("\0")
230 const existing = groups.get(key) ?? []
231 existing.push(row)
232 groups.set(key, existing)
233 }
234 return [...groups.values()].map((rows) => {
235 const result: Record<string, unknown> = {}
236 for (const fi of fieldItems) result[fi.key] = get(rows[0]!, fi.path)
237 for (const ai of aggregateItems) result[ai.key] = computeAggregateValue(rows, ai.aggregate)
238 return result as M
239 })
240 }
241
242 return r.map((i) => {
243 const [keys, entries] = pipe(
244 sel,
245 Array.partition((entry) => typeof entry === "string" ? Result.fail(String(entry)) : Result.succeed(entry))
246 )
247 const subKeys = entries.filter((entry): entry is { key: string; subKeys: readonly string[] } =>
248 typeof entry === "object" && entry !== null && "subKeys" in entry
249 )
250 const computedKeys = entries.filter((entry): entry is {
251 key: string
252 computed: ComputedProjectionIrExpression
253 } => typeof entry === "object" && entry !== null && "computed" in entry)
254 const pathKeys = entries.filter((entry): entry is { key: string; path: string } =>
255 typeof entry === "object" && entry !== null && "path" in entry && !("aggregate" in entry)
256 )
257 const n = Struct.pick(i, keys)
258 subKeys.forEach((subKey) => {
259 n[subKey.key] = i[subKey.key]!.map(Struct.pick(subKey.subKeys as never[]))
260 })
261 computedKeys.forEach((entry) => {
262 ;(n as Record<string, unknown>)[entry.key] = computeProjectionValue(i, entry.computed)
263 })
264 pathKeys.forEach((entry) => {
265 ;(n as Record<string, unknown>)[entry.key] = get(i, entry.path)
266 })

Callers 3

query.test.tsFile · 0.85
runCFFunction · 0.85
makeMemoryStoreIntFunction · 0.85

Calls 10

getFunction · 0.90
codeFilterFunction · 0.90
codeFilter3_Function · 0.90
selectPerRowFunction · 0.85
someMethod · 0.80
filterMethod · 0.80
mapMethod · 0.65
makeMethod · 0.65
pipeMethod · 0.65
takeMethod · 0.65

Tested by 1

runCFFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…