@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
64 lines (63 loc) • 2.15 kB
TypeScript
import { ErrorMode, Mapper, Predicate } from '@naturalcycles/js-lib';
import { TransformTyped } from '../stream.model';
export interface TransformMapOptions<OUT = any> {
/**
* @default true
*/
objectMode?: boolean;
/**
* @default false
* Set true to support "multiMap" - possibility to return [] and emit 1 result for each item in the array.
*/
flattenArrayOutput?: boolean;
/**
* Predicate to filter outgoing results (after mapper).
* Allows to not emit all results.
*
* @default to filter out undefined/null values, but pass anything else
*
* Set to `r => r` (passthrough predicate) to pass ANY value (including undefined/null)
*/
predicate?: Predicate<OUT>;
/**
* Number of concurrently pending promises returned by `mapper`.
*
* @default 16 (to match default highWatermark option for objectMode streams)
*/
concurrency?: number;
/**
* @default THROW_IMMEDIATELY
*/
errorMode?: ErrorMode;
/**
* If defined - will be called on every error happening in the stream.
* Called BEFORE observable will emit error (unless skipErrors is set to true).
*/
onError?: (err: Error) => any;
/**
* Progress metric
* @default `stream`
*/
metric?: string;
/**
* If defined - called BEFORE `final()` callback is called.
*/
beforeFinal?: () => any;
/**
* If defined - called AFTER final chunk was processed.
*/
afterFinal?: () => any;
}
/**
* Like pMap, but for streams.
* Inspired by `through2`.
* Main feature is concurrency control (implemented via `through2-concurrent`) and convenient options.
* Using this allows native stream .pipe() to work and use backpressure.
*
* Only works in objectMode (due to through2Concurrent).
*
* Concurrency defaults to 16.
*
* If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
*/
export declare function transformMap<IN = any, OUT = IN>(mapper: Mapper<IN, OUT>, opt?: TransformMapOptions<OUT>): TransformTyped<IN, OUT>;