Skip to content

Commit

Permalink
perf: do quote replacement in a single pass
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 27, 2021
1 parent e1c8be1 commit 5f001b7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ class JSON2CSVBase {
}

if (typeof value === 'string') {
if(value.includes(this.opts.quote)) {
value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote);
}

if (this.opts.excelStrings) {
value = `"=""${value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote)}"""`;
if(value.includes(this.opts.quote)) {
value = value.replace(new RegExp(this.opts.quote, 'g'), `${this.opts.escapedQuote}${this.opts.escapedQuote}`);
}
value = `"=""${value}"""`;
} else {
if(value.includes(this.opts.quote)) {
value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote);
}
value = `${this.opts.quote}${value}${this.opts.quote}`;
}
}
Expand Down

0 comments on commit 5f001b7

Please sign in to comment.