@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
34 lines (31 loc) • 817 B
text/typescript
import { Transform } from 'node:stream'
import type { TransformTyped } from '../stream.model.js'
export function transformFlatten<T>(): TransformTyped<T[], T> {
return new Transform({
objectMode: true,
highWaterMark: 1,
transform(chunk: T[], _, cb) {
for (const item of chunk) {
this.push(item)
}
cb() // acknowledge
},
})
}
export function transformFlattenIfNeeded<T>(): TransformTyped<T[], T> {
return new Transform({
objectMode: true,
highWaterMark: 1,
transform(chunk: T[], _, cb) {
if (Array.isArray(chunk)) {
for (const item of chunk) {
this.push(item)
}
} else {
// As a safety precaution, to not crash the pipeline - push as is
this.push(chunk)
}
cb() // acknowledge
},
})
}