From 5f001b75dd9dd2df36e03a314a21d3476568ce3d Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Wed, 27 Jan 2021 20:39:29 +0100 Subject: [PATCH] perf: do quote replacement in a single pass --- lib/JSON2CSVBase.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 4444fe9..6e45da2 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -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}`; } }