-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Performance improvements and new async api (#360)
* Update dependencies * Memoize preprocessRow * Skip preprocessing if no preprocessing options enabled * Improve Regexps * Remove unnecessary intermediate variable * Replace indexOf by includes * Improve fields gathering * Fix a bug that created nested field getters if reusing the same parser * Flatten using push and fallback to concat * Add new Async Parser * Add tests for new Async Parser * Ensure that async tests finish properly even when there are exceptions * Create promise before pushing the data in the AsyncParser
- Loading branch information
1 parent
58bd0ae
commit d59dea1
Showing
16 changed files
with
4,453 additions
and
3,169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const { Transform } = require('stream'); | ||
const JSON2CSVTransform = require('./JSON2CSVTransform'); | ||
|
||
class JSON2CSVAsyncParser { | ||
constructor(opts, transformOpts) { | ||
this.input = new Transform(transformOpts); | ||
this.input._read = () => {}; | ||
|
||
this.transform = new JSON2CSVTransform(opts, transformOpts); | ||
this.processor = this.input.pipe(this.transform); | ||
} | ||
|
||
fromInput(input) { | ||
if (this._input) { | ||
throw new Error('Async parser already has an input.'); | ||
} | ||
this._input = input; | ||
this.input = this._input.pipe(this.processor); | ||
return this; | ||
} | ||
|
||
throughTransform(transform) { | ||
if (this._output) { | ||
throw new Error('Can\'t add transforms once an output has been added.'); | ||
} | ||
this.processor = this.processor.pipe(transform); | ||
return this; | ||
} | ||
|
||
toOutput(output) { | ||
if (this._output) { | ||
throw new Error('Async parser already has an output.'); | ||
} | ||
this._output = output; | ||
this.processor = this.processor.pipe(output); | ||
return this; | ||
} | ||
|
||
promise() { | ||
return new Promise((resolve, reject) => { | ||
let csv = ''; | ||
this.processor | ||
.on('data', chunk => (csv += chunk.toString())) | ||
.on('finish', () => resolve(csv)) | ||
.on('error', err => reject(err)); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = JSON2CSVAsyncParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.