Skip to content

Commit f6bfa50

Browse files
committed
feat: use jsonparse for ND-JSON instead of the custom made implementation
1 parent 471f5a7 commit f6bfa50

File tree

1 file changed

+0
-43
lines changed

1 file changed

+0
-43
lines changed

lib/JSON2CSVTransform.js

-43
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class JSON2CSVTransform extends Transform {
1919

2020
if (this._readableState.objectMode) {
2121
this.initObjectModeParse();
22-
} else if (this.opts.ndjson) {
23-
this.initNDJSONParse();
2422
} else {
2523
this.initJSONParser();
2624
}
@@ -52,47 +50,6 @@ class JSON2CSVTransform extends Transform {
5250
};
5351
}
5452

55-
/**
56-
* Init the transform with a parser to process NDJSON data.
57-
* It maintains a buffer of received data, parses each line
58-
* as JSON and send it to `pushLine for processing.
59-
*/
60-
initNDJSONParse() {
61-
const transform = this;
62-
63-
this.parser = {
64-
_data: '',
65-
write(chunk) {
66-
this._data += chunk.toString();
67-
const lines = this._data
68-
.split('\n')
69-
.map(line => line.trim())
70-
.filter(line => line !== '');
71-
72-
let pendingData = false;
73-
lines
74-
.forEach((line, i) => {
75-
try {
76-
transform.pushLine(JSON.parse(line));
77-
} catch(e) {
78-
if (i === lines.length - 1) {
79-
pendingData = true;
80-
} else {
81-
e.message = `Invalid JSON (${line})`
82-
transform.emit('error', e);
83-
}
84-
}
85-
});
86-
this._data = pendingData
87-
? this._data.slice(this._data.lastIndexOf('\n'))
88-
: '';
89-
},
90-
getPendingData() {
91-
return this._data;
92-
}
93-
};
94-
}
95-
9653
/**
9754
* Init the transform with a parser to process JSON data.
9855
* It maintains a buffer of received data, parses each as JSON

0 commit comments

Comments
 (0)