@giancosta86/flatten-transform
v1.0.0
Published
Modern, configurable flattening stream
Downloads
2
Maintainers
Readme
flatten-transform
Modern, configurable flattening stream
flatten-transform provides a FlattenTransform
stream for NodeJS, flattening arrays and other iterables into a linear sequence of items - although the client can optionally set a maximum nesting level so as to constrain the recursive algorithm.
Like any other standard Transform
object, the stream can be plugged into a pipeline, as well as manually controlled.
Installation
npm install @giancosta86/flatten-transform
or
yarn add @giancosta86/flatten-transform
Usage
Just create a new instance of FlattenTransform
and use it in a pipeline, or call its standard methods like .write()
.end()
, .on()
, ...
Example
This somehow contrived example still shows the simplicity of FlattenTransform
as well as its support for pipelines and events - among the various stream features.
export async function recursiveFlatten(
source: Iterable<unknown>
): Promise<unknown[]> {
const linearResult: unknown[] = [];
const flattenTransform = new FlattenTransform().on("data", item =>
linearResult.push(item)
);
await pipeline(Readable.from(source), flattenTransform);
return Promise.resolve(linearResult);
}
Constructor parameters
maxNestingLevel: a limit to the recursion performed by the flattening algorithm. The default value is
Infinity
- meaning that each and every iterable will be recursively flattened; in contrast, the minimum allowed value -0
- will actually disable flattening, convertingFlattenTransform
to a pass-through streamhighWaterMark: if present, passed to the base constructor
signal: if present, passed to the base constructor
Further reference
For additional examples, please consult the unit tests in the source code repository.