Files
strom/src/functions/flatMap.ts
Jerry Kurian a11aa10d16 Clean up
2019-09-26 09:23:09 -04:00

19 lines
506 B
TypeScript

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