@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
28 lines (26 loc) • 736 B
text/typescript
import { map } from '@johngw/stream/transformers/map'
import { Accumulator } from '@johngw/stream-common/Function'
/**
* 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<I, O>(acc: O, fn: Accumulator<I, O>) {
return map<I, O>(async (chunk) => {
acc = await fn(acc, chunk)
return acc
})
}