@push-stream-std/push-recursive-flatten (0.0.6)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-recursive-flatten@0.0.6"@push-stream-std/push-recursive-flatten": "0.0.6"About this package
@push-stream-std/push-recursive-flatten
Recursively flatten nested push-streams up to a given depth.
Published as @push-stream-std/push-recursive-flatten.
Synopsis
import { flatten } from '@push-stream-std/push-recursive-flatten';
import { values } from '@push-stream-std/push-pushable';
import { collect } from '@push-stream-std/push-concat';
const inner = values(['c', 'd']);
const outer = values([inner, 'e']);
const nested = values([outer, 'f']);
nested
.pipe(flatten(2))
.pipe(collect((err, results) => {
console.log(results); // ['c', 'd', 'e', 'f']
}));
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-recursive-flatten
Why
A push-stream through operator that recursively flattens nested stream values. When a value is itself a push-stream source (detected by the presence of pipe, write, or resume), it is piped into the flattening pipeline and its values are emitted inline. The depth parameter controls how many levels of nesting are unwrapped. Uses O(1) amortized internal buffering via a head pointer pattern. Non-stream values pass through unchanged.
API
flatten([depth])
depth(number, defaultInfinity) — Maximum nesting depth to flatten. Atdepth0, streams are treated as plain values and passed through. Atdepth1, one level of nesting is flattened, and so on.
Returns a push-stream through. Pipe a source in; any value that is a push-stream source will be flattened inline up to the specified depth.
Tests
npm test
Tests cover flattening single-level and multi-level nested streams, respecting the depth parameter, mixed stream and non-stream values, and empty nested streams.
Requirements
Node.js 22 or later. ESM only.