ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Support output to stream or string #12

Closed pke closed 6 years ago

pke commented 6 years ago

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?

ryu1kn commented 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:

pke commented 6 years ago

This looks good, thanks for the swift reply. How could I not see it in the readme?

offero commented 4 years ago

This would be a great utility to include in the lib.

ryu1kn commented 4 years ago

@offero I've been thinking about that too 😀

ryu1kn commented 4 years ago

Another instance where not providing the file write part in one step could have been better: https://github.com/ryu1kn/csv-writer/issues/43

ryu1kn commented 3 years ago

Yet another one: https://github.com/ryu1kn/csv-writer/issues/71