Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions lib/core/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Transform = require('stream').Transform;
class Stream extends Transform {

constructor(parser) {
super();
super({ objectMode: parser._options.objectMode });
this._parser = parser;
this._options = parser._options;
this._headers = this._options.headers || [];
Expand All @@ -25,7 +25,7 @@ class Stream extends Transform {
if (self._extra.length > 0) self._extra = "";
return chunk;
}

_wrapArray(data) {
if (data.charAt(0) != '[') data = '[' + data;
if (data.charAt(data.length - 1) != ']') data += ']';
Expand All @@ -35,22 +35,27 @@ class Stream extends Transform {
_transform(chunk, encoding, done) {
let self = this;
let json = null;
// Append extra data to chunk data
chunk = this._mergeChunk(chunk);
if (!chunk) return done(this._lastError);
// Split chunk in objects
let parts = chunk.split('}');
while (json === null && parts.length > 0) {
try {
let data = self._wrapArray(parts.join('}'));
json = JSON.parse(data);
} catch (ex) {
this._lastError = ex;
let extraChunk = parts.pop();
self._extra = extraChunk + (self._extra || "");
if (parts.length > 0) parts[parts.length - 1] += "}";
if (this._options.objectMode) {
json = Array.isArray(chunk) ? chunk : [chunk];
}
else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this else to keep this part less indented

// Append extra data to chunk data
chunk = this._mergeChunk(chunk);
if (!chunk) return done(this._lastError);
// Split chunk in objects
let parts = chunk.split('}');
while (json === null && parts.length > 0) {
try {
let data = self._wrapArray(parts.join('}'));
json = JSON.parse(data);
} catch (ex) {
this._lastError = ex;
let extraChunk = parts.pop();
self._extra = extraChunk + (self._extra || "");
if (parts.length > 0) parts[parts.length - 1] += "}";
}
}
}
}
if (!json) return done();
this._parser.parse(json, (err, csvChunk) => {
if (err) return done(err);
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function() {
fillGaps: false, // Boolean
verticalOutput: true, // Boolean
forceTextDelimiter: false, //Boolean
objectMode: false, // Boolean
};
// argument parsing
let json, userOptions, callback;
Expand Down