Skip to content

Commit

Permalink
refactor: rework JSON2CSVStreamParser to use callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 22, 2022
1 parent d49cff4 commit 50ed883
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
40 changes: 24 additions & 16 deletions lib/JSON2CSVStreamParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JSON2CSVStreamParser extends JSON2CSVBase {
getObjectModeTokenizer() {
return {
write: (data) => this.pushLine(data),
end: () => {},
end: () => this.onEnd(),
};
}

Expand All @@ -48,11 +48,30 @@ class JSON2CSVStreamParser extends JSON2CSVBase {
} else if (token === TokenType.LEFT_BRACE) {
this.tokenParser = new TokenParser({ paths: ['$'], keepStack: false });
} else {
throw new Error('Data should not be empty or the "fields" option should be included');
this.onError(new Error('Data should not be empty or the "fields" option should be included'));
return;
}

tokenizer.onToken = this.tokenParser.write.bind(this.tokenParser);
tokenizer.onError = this.onError.bind(this);
tokenizer.onEnd = () => {
if (!this.tokenParser.isEnded) this.tokenParser.end();
};

this.tokenParser.onValue = (value) => this.pushLine(value);
this.tokenParser.onError = this.onError.bind(this);
this.tokenParser.onEnd = () => {
if (!this.tokenizer.isEnded) this.tokenizer.end();
if (!this._hasWritten) {
if (!this.opts.fields) {
this.onError(new Error('Data should not be empty or the "fields" option should be included'));
return;
}

this.pushHeader();
}
this.onEnd();
};
}

this.tokenParser.write(token, value, offset);
Expand All @@ -66,27 +85,15 @@ class JSON2CSVStreamParser extends JSON2CSVBase {
}

end() {
this.tokenizer.end();
if (this.tokenParser && !this.tokenParser.isEnded) {
this.tokenParser.end();
}
this.onEnd();

if (!this._hasWritten) {
if (!this.opts.fields) {
throw new Error('Data should not be empty or the "fields" option should be included');
}

this.pushHeader();
}
if (this.tokenizer && !this.tokenizer.isEnded) this.tokenizer.end();
}

/**
* Generate the csv header and pushes it downstream.
*/
pushHeader() {
if (this.opts.withBOM) {
this.push('\ufeff');
this.onData('\ufeff');
}

if (this.opts.header) {
Expand Down Expand Up @@ -124,6 +131,7 @@ class JSON2CSVStreamParser extends JSON2CSVBase {
onHeader(header) {}
onLine(line) {}
onData(data) {}
onError() {}
onEnd() {}
/* eslint-enable no-unused-vars */
}
Expand Down
6 changes: 5 additions & 1 deletion lib/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ class JSON2CSVTransform extends Transform {
this.push(data);
}

onError(err) {
throw err;
}

onEnd() {
this.end.bind(this);
if (!this.writableEnded) this.end();
}

/**
Expand Down

0 comments on commit 50ed883

Please sign in to comment.