@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
32 lines (31 loc) • 846 B
JavaScript
import { Transform } from 'node:stream';
export function transformFlatten() {
return new Transform({
objectMode: true,
highWaterMark: 1,
transform(chunk, _, cb) {
for (const item of chunk) {
this.push(item);
}
cb(); // acknowledge
},
});
}
export function transformFlattenIfNeeded() {
return new Transform({
objectMode: true,
highWaterMark: 1,
transform(chunk, _, 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
},
});
}