Skip to content

@jsvision/datagrid / foldAggregate

Function: foldAggregate()

foldAggregate(fn, values): number | undefined

Defined in: datagrid/src/aggregate.ts:72

Reduce values (one per displayed row — the column's typed value(row)) by fn.

The numeric folds (sum/avg/min/max) include a value only when it is a finite number (typeof value === 'number' && Number.isFinite(value)) — null/undefined/NaN/±Infinity and any non-number are skipped, so one bad cell never poisons the total to NaN. count counts every row (all values), null or not. avg divides by the count of the finite contributors only.

Parameters

fn

AggregateFn

The reduction to apply.

values

Iterable<unknown>

The column's values across the displayed rows (any type; non-finite entries are skipped by the numeric folds).

Returns

number | undefined

The numeric result, or undefined for an empty contributor set on avg/min/max (which the renderer paints as a blank cell). sum and count of an empty set return 0.

Example

ts
import { foldAggregate } from '@jsvision/datagrid';
foldAggregate('sum', [10, 20, null, 30]);       // 60  (null skipped)
foldAggregate('avg', [10, 20, null]);            // 15  (30 / 2 finite)
foldAggregate('max', []);                        // undefined → blank cell
foldAggregate('count', [1, null, 3]);            // 3   (counts rows)