@future-widget-lab/record-set
Version:
A dedicated data structure for in-memory record collections, offering fluent, immutable APIs for MongoDB-like querying, sorting, and transformation.
24 lines (19 loc) • 597 B
text/typescript
type ReduceOptions<TRecord, TAccumulator> = {
reducer: (accumulator: TAccumulator, record: TRecord) => TAccumulator;
initialValue: TAccumulator;
records: Array<TRecord>;
};
/**
* @description
* Use this helper to reduce the record set to a single accumulated value.
*/
export const reduce = <TRecord, TAccumulator>(
options: ReduceOptions<TRecord, TAccumulator>
): TAccumulator => {
const { reducer, initialValue, records } = options;
let accumulator = initialValue;
for (const record of records) {
accumulator = reducer(accumulator, record);
}
return accumulator;
};