Closed pke closed 6 years ago
Hi @pke , we already have CsvStringifier
, its usage is on the README. Does it suffice your needs?
With this, you can also easily create a transform stream like this:
const {Transform} = require('stream');
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;
class PeopleTransformStream extends Transform {
constructor() {
super({objectMode: true});
this._firstRecord = true;
this._csvStringifier = createCsvStringifier({
header: [
{id: 'name', title: 'NAME'},
{id: 'lang', title: 'LANGUAGE'}
]
});
}
// Here I'm assuming readable stream gives me people one-by-one (i.e. person)
// If the readable stream passes multiple people, change it accordingly.
_transform(person, encoding, callback) {
const personLine = this._csvStringifier.stringifyRecords([person]);
if (this._firstRecord) {
this._firstRecord = false;
callback(null, this._csvStringifier.getHeaderString() + personLine);
} else {
callback(null, personLine);
}
}
}
And if you already have readable & writable streams, you can pipe them
const transformStream = new PeopleTransformStream();
readStream.pipe(transformStream).pipe(writeStream);
Just make sure your readable stream is also working with objectMode
:wink:
This looks good, thanks for the swift reply. How could I not see it in the readme?
This would be a great utility to include in the lib.
@offero I've been thinking about that too 😀
Another instance where not providing the file write part in one step could have been better: https://github.com/ryu1kn/csv-writer/issues/43
Yet another one: https://github.com/ryu1kn/csv-writer/issues/71
Sometimes (in a webserver scenario) one does not need to write a file. Instead the CSV could be streamed or written to a string. Possible to add this here?