formatted-stream
A universal wrapper around file format parsers.
Currently supports in- and output in CSV, JSON, and XLSX format.
Usage
Install formatted-stream:
npm install formatted-stream --save
Include and use in your code:
ES5
const fs = formattedStream = default parser = formattedStream writer = formattedStream; parser;writer;fs;
ES6
;; const parser = formattedStream writer = formattedStream; parser;writer;fs;
API Documentation
.from(format, options)
const parser = formattedStream;
format
Type String
. Must be either xlsx, json, or csv.
options.transform
Type Function
. A transformation function which is applied to each object before .on('data')
is triggered.
Default is function(data) { return data; }
.
options.csv
Type Object
. An options object which will be passed to the CSV parser.
See fast-csv for more information.
Default is {headers: true}
.
options.json
Type Object
. The value of options.json.path
will be passed to JSONStream.parse
.
See JSONStream for more information.
Default is {path: [true]}
.
options.xlsx
Type Object
. An options object which will be passed to the XLSX parser.
Set the XLSX worksheet by defining options.xlsx.sheet
. Default: Sheet 1
See ExcelJS for more options.
Default is {sheet: 'Sheet 1'}
.
.to(format, options)
const writer = formattedStream;
format
Type String
. Must be either xlsx, json, or csv.
options.transform
Type Function
. A transformation function which is applied to each object before .on('data')
is triggered.
Default is function(data) { return data; }
.
options.csv
Type Object
. An options object which will be passed to the CSV writer.
See fast-csv for more information.
Default is {}
.
options.json
Type Object
. An options object which will be passed to the JSON writer.
See JSONStream for more information.
Default is {}
.
options.xlsx
Type Object
. An options object which will be passed to the XLSX writer.
Assign a function to options.xlsx.headerAccessor
in order to transform all header fields according to the output of the function. Set the XLSX worksheet by defining options.xlsx.sheet
. If no sheet is defined, the XLSX parser will write all sheets in the document to the CSV file. Default: undefined
.
See ExcelJS for more options.
Default is {}
.