UNPKG

627 BPlain TextView Raw
1import { Transform } from 'stream'
2import { TransformOptions, TransformTyped } from '../stream.model'
3
4/**
5 * Will collect all stream results in the array (keeping it in memory) and emit in the end as one result.
6 */
7export function transformToArray<IN>(opt: TransformOptions = {}): TransformTyped<IN, IN[]> {
8 const res: IN[] = []
9
10 return new Transform({
11 objectMode: true,
12 ...opt,
13 transform(chunk: IN, _, cb) {
14 res.push(chunk)
15 // callback to signal that we processed input, but not emitting any output
16 cb()
17 },
18 final(this: Transform, cb) {
19 this.push(res)
20 cb()
21 },
22 })
23}