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

Function count

packages/db-ivm/src/operators/groupBy.ts:169–183  ·  view source on GitHub ↗
(
  valueExtractor: (value: T) => any = (v) => v,
)

Source from the content-addressed store, hash-verified

167 * Creates a count aggregate function
168 */
169export function count<T>(
170 valueExtractor: (value: T) => any = (v) => v,
171): AggregateFunction<T, number, number> {
172 return {
173 // Count only not-null values (the `== null` comparison gives true for both null and undefined)
174 preMap: (data: T) => (valueExtractor(data) == null ? 0 : 1),
175 reduce: (values: Array<[number, number]>) => {
176 let totalCount = 0
177 for (const [nullMultiplier, multiplicity] of values) {
178 totalCount += nullMultiplier * multiplicity
179 }
180 return totalCount
181 },
182 }
183}
184
185/**
186 * Creates an average aggregate function

Callers

nothing calls this directly

Calls 1

valueExtractorFunction · 0.50

Tested by

no test coverage detected