Skip to content

Commit

Permalink
feat: use jsonparse for ND-JSON instead of the custom made implementa…
Browse files Browse the repository at this point in the history
…tion (#493)
  • Loading branch information
juanjoDiaz authored Nov 2, 2020
1 parent 471f5a7 commit 55aa0c7
Showing 1 changed file with 0 additions and 43 deletions.
43 changes: 0 additions & 43 deletions lib/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class JSON2CSVTransform extends Transform {

if (this._readableState.objectMode) {
this.initObjectModeParse();
} else if (this.opts.ndjson) {
this.initNDJSONParse();
} else {
this.initJSONParser();
}
Expand Down Expand Up @@ -52,47 +50,6 @@ class JSON2CSVTransform extends Transform {
};
}

/**
* Init the transform with a parser to process NDJSON data.
* It maintains a buffer of received data, parses each line
* as JSON and send it to `pushLine for processing.
*/
initNDJSONParse() {
const transform = this;

this.parser = {
_data: '',
write(chunk) {
this._data += chunk.toString();
const lines = this._data
.split('\n')
.map(line => line.trim())
.filter(line => line !== '');

let pendingData = false;
lines
.forEach((line, i) => {
try {
transform.pushLine(JSON.parse(line));
} catch(e) {
if (i === lines.length - 1) {
pendingData = true;
} else {
e.message = `Invalid JSON (${line})`
transform.emit('error', e);
}
}
});
this._data = pendingData
? this._data.slice(this._data.lastIndexOf('\n'))
: '';
},
getPendingData() {
return this._data;
}
};
}

/**
* Init the transform with a parser to process JSON data.
* It maintains a buffer of received data, parses each as JSON
Expand Down

0 comments on commit 55aa0c7

Please sign in to comment.