Skip to content

Commit

Permalink
fix: Escape custom quotes correctly (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed May 23, 2018
1 parent 330b566 commit 7d57208
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,24 @@ class JSON2CSVBase {
.replace(/\u2029/g, '\r');
}

//Replace single quote with double quote. Single quote are preceeded by
//a backslash, and it's not at the end of the stringifiedValue.
stringifiedValue = stringifiedValue
.replace(/^"(.*)"$/, this.opts.quote + '$1' + this.opts.quote)
.replace(/(\\")(?!$)/g, this.opts.doubleQuote)
.replace(/\\\\/g, '\\');

if (this.opts.quote === '"') {
// Replace automatically scaped single quotes by doubleQuotes
stringifiedValue = stringifiedValue
.replace(/(\\")(?!$)/g, this.opts.doubleQuote);
} else {
// Unescape automatically escaped double quote symbol
// Replace wrapping quotes
// Replace single quote with double quote
stringifiedValue = stringifiedValue
.replace(/(\\")(?!$)/g, '"')
.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote)
.replace(/^"(.*)"$/, this.opts.quote + '$1' + this.opts.quote);
}

// Remove double backslashes
stringifiedValue = stringifiedValue
.replace(/\\\\/g, '\\');

if (this.opts.excelStrings && typeof value === 'string') {
stringifiedValue = '"="' + stringifiedValue + '""';
Expand Down
11 changes: 11 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should escape quotes when setting \'quote\' property is present', (t) => {
const opts = ' --fields carModel,color --quote "\'"';

child_process.exec(cli + '-i ' + getFixturePath('/json/escapeCustomQuotes.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.escapeCustomQuotes);
t.end();
});
});

// Double Quote

testRunner.add('should escape quotes with double quotes', (t) => {
Expand Down
13 changes: 13 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should escape quotes when setting \'quote\' property is present', (t) => {
const opts = {
fields: ['carModel', 'color'],
quote: '\''
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.escapeCustomQuotes);

t.equal(csv, csvFixtures.escapeCustomQuotes);
t.end();
});

// Double Quote

testRunner.add('should escape quotes with double quotes', (t) => {
Expand Down
19 changes: 19 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,25 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should escape quotes when setting \'quote\' property is present', (t) => {
const opts = {
fields: ['carModel', 'color'],
quote: '\''
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.escapeCustomQuotes().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.escapeCustomQuotes);
t.end();
})
.on('error', err => t.notOk(true, err.message));
});

// Double Quote

testRunner.add('should escape quotes with double quotes', (t) => {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/csv/escapeCustomQuotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'carModel','color'
'''Audi''','''blue'''
3 changes: 3 additions & 0 deletions test/fixtures/json/escapeCustomQuotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "carModel": "'Audi'", "price": 0, "color": "'blue'" }
]

0 comments on commit 7d57208

Please sign in to comment.