UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

26 lines 694 B
import { map } from '@johngw/stream/transformers/map'; /** * Every chunk is transformed in to an accumulator. * * @group Transformers * @see {@link reduce:function} * @example * Accumulate all previous chunks with a counter. * ``` * --0-------------------------1------------------------------- * * accumulate({ chunks: [], counter: 0 }, (acc, chunk) => ({ * chunks: [...acc.chunks, chunk], * counter: acc.counter + 1 * })) * * ----{chunks:[0],counter:1}------{chunks:[0,1],counter:2}---- * ``` */ export function accumulate(acc, fn) { return map(async (chunk) => { acc = await fn(acc, chunk); return acc; }); } //# sourceMappingURL=accumulate.js.map