-
-
Notifications
You must be signed in to change notification settings - Fork 2
Main module
Eugene Lazutkin edited this page Jun 21, 2018
·
1 revision
The main module returns a factory function, which produces instances of Parser decorated with stream-json's emit().
const makeParser = require('stream-csv-as-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.csv').pipe(makeParser());
let rowCounter = 0;
pipeline.on('startArray', () => ++rowCounter);
pipeline.on('end', console.log(`Found ${rowCounter} rows.`));
The returned factory function takes one optional argument: options, and returns a new instance of Parser
.
The whole implementation of this function is very simple:
const make = options => emit(new Parser(options));
It is set to Parser.parser().
Note that instances produced with parser()
are not decorated by emit(). You may want to use it in order to avoid a possible overhead.
const {parser} = require('stream-csv-as-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.csv').pipe(parser());
let rowCounter = 0;
pipeline.on('data', data => data.name === 'startArray' && ++rowCounter);
pipeline.on('end', console.log(`Found ${rowCounter} rows.`));
It is set to Parser.
Note that instances produced with new Parser()
are not decorated by emit(). You may want to use it in order to avoid a possible overhead.
const {Parser} = require('stream-csv-as-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.csv').pipe(new Parser());
let rowCounter = 0;
pipeline.on('data', data => data.name === 'startArray' && ++rowCounter);
pipeline.on('end', console.log(`Found ${rowCounter} rows.`));