@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
24 lines (21 loc) • 627 B
text/typescript
import { Transform } from 'stream'
import { TransformOptions, TransformTyped } from '../stream.model'
/**
* Will collect all stream results in the array (keeping it in memory) and emit in the end as one result.
*/
export function transformToArray<IN>(opt: TransformOptions = {}): TransformTyped<IN, IN[]> {
const res: IN[] = []
return new Transform({
objectMode: true,
...opt,
transform(chunk: IN, _, cb) {
res.push(chunk)
// callback to signal that we processed input, but not emitting any output
cb()
},
final(this: Transform, cb) {
this.push(res)
cb()
},
})
}