-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support output to stream or string #12
Comments
Hi @pke , we already have 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 |
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: #43 |
Yet another one: #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?
The text was updated successfully, but these errors were encountered: