import { Transform, TransformOptions } from "stream"; export function flatMap( mapper: | ((chunk: T, encoding: string) => R[]) | ((chunk: T, encoding: string) => Promise), options?: TransformOptions, ): Transform { return new Transform({ ...options, async transform(chunk: T, encoding, callback) { (await mapper(chunk, encoding)).forEach(c => this.push(c)); callback(); }, }); }