@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
30 lines • 840 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.accumulate = void 0;
const map_1 = require("@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}----
* ```
*/
function accumulate(acc, fn) {
return (0, map_1.map)(async (chunk) => {
acc = await fn(acc, chunk);
return acc;
});
}
exports.accumulate = accumulate;
//# sourceMappingURL=accumulate.js.map