push-stream-std

@push-stream-std/push-scan (0.0.7)

Published 2026-07-21 04:00:30 +00:00 by Dvorak

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.7
"@push-stream-std/push-scan": "0.0.7"

About this package

@push-stream-std/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-pushable';
import { collect } from '@push-stream-std/push-concat';

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

Why

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])

  • reducerfunction(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 22 or later. ESM only.

Keywords

push-stream stream scan reduce
Details
npm
2026-07-21 04:00:30 +00:00
0
SEE LICENSE IN LICENSE
latest
3.4 KiB
Assets (1)
Versions (5) View all
0.0.7 2026-07-21
0.0.6 2026-07-20
0.0.5 2026-07-20
0.0.4 2026-07-19
0.0.3 2026-07-19