@push-stream-std/push-scan (0.0.5)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-scan@0.0.5"@push-stream-std/push-scan": "0.0.5"About this package
push-scan
Running reduction, emitting every intermediate accumulator value.
Published as @push-stream-std/push-scan.
Synopsis
import { scan } from '@push-stream-std/push-scan';
import { values } from '@push-stream-std/push-values';
import { collect } from '@push-stream-std/push-collect';
values([1, 2, 3, 4])
.pipe(scan((acc, n) => acc + n, 0))
.pipe(collect((err, results) => {
console.log(results); // [1, 3, 6, 10]
}));
Without initial value:
values([5, 10, 15])
.pipe(scan((acc, n) => acc + n))
.pipe(collect((err, results) => {
console.log(results); // [15, 30]
}));
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-scan
Description
A push-stream through operator that performs a running reduction over incoming values and emits each intermediate result. When an initial value is provided, the reducer is called with reducer(initial, firstValue) and that result is emitted as the first output. Without an initial value, the first incoming value becomes the initial accumulator and is not emitted; subsequent values are reduced against it and emitted. If the reducer throws, the stream is aborted with the error.
API
scan(reducer, [initial])
reducer—function(accumulator, value)— Reduction function. Receives the current accumulator and the incoming value. Must return the new accumulator.initial(any, optional) — Seed value for the accumulator. If omitted, the first incoming value is used as the seed and is not emitted.
Returns a push-stream through. Each write that triggers a reduction emits the new accumulator value downstream.
Tests
npm test
Tests cover scan with initial value, scan without initial value (first value becomes seed), incremental accumulation, and reducer error handling.
Requirements
Node.js 18 or later. ESM only.
License
MIT