-
-
Notifications
You must be signed in to change notification settings - Fork 51
Main module
Eugene Lazutkin edited this page Jun 18, 2018
·
6 revisions
The main module returns a factory function, which produces instances of Parser decorated with emit().
const makeParser = require('stream-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(makeParser());
let objectCounter = 0;
pipeline.on('startObject', () => ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));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-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(parser());
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));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-json');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(new Parser());
let objectCounter = 0;
pipeline.on('data', data => data.name === 'startObject' && ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));