A CSV encoder stream that produces properly escaped CSVs


A CSV encoder stream that produces properly escaped CSVs.

A through stream. Write arrays of strings (or JS objects) and you will receive a properly escaped CSV stream out the other end.

var csvWriter = require('csv-write-stream')
var writer = csvWriter()

writer is a duplex stream -- you can pipe data to it and it will emit a string for each line of the CSV

  separator: ',',
  newline: '\n',
  headers: undefined,
  sendHeaders: true

headers can be an array of strings to use as the header row. if you don't specify a header row the keys of the first row written to the stream will be used as the header row IF the first row is an object (see the test suite for more details). if the sendHeaders option is set to false, the headers will be used for ordering the data but will never be written to the stream.

example of auto headers:

var writer = csv()
writer.write({hello: "world", foo: "bar", baz: "taco"})
// produces: hello,foo,baz\nworld,bar,taco\n 

example of specifying headers:

var writer = csv({ headers: ["hello", "foo"]})
writer.write(['world', 'bar'])
// produces: hello,foo\nworld,bar\n 

example of not sending headers:

var writer = csv({sendHeaders: false})
writer.write({hello: "world", foo: "bar", baz: "taco"})
// produces: world,bar,taco\n 

see the test suite for more examples

$ npm install
$ npm test

This module also includes a CLI, which you can pipe ndjson to stdin and it will print csv on stdout. You can install it with npm install -g csv-write-stream.

cat example.ndjson | csv-write-stream > example.csv