Skip to content
Merged
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
12 changes: 7 additions & 5 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,16 @@ class JSON2CSVBase {
.replace(/\u21E5/g, '\t');
}

// Replace automatically scaped single quotes by doubleQuotes
stringifiedValue = stringifiedValue
.replace(/\\"(?!$)/g, this.opts.doubleQuote);

if (this.opts.quote !== '"') {
if (this.opts.quote === '"') {
// Replace automatically escaped single quotes by doubleQuotes
stringifiedValue = stringifiedValue
.replace(/\\"(?!$)/g, this.opts.doubleQuote);
} else {
// Unescape double quotes ('"')
// Replace single quote with double quote
// Replace wrapping quotes
stringifiedValue = stringifiedValue
.replace(/\\"(?!$)/g, '"')
.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote)
.replace(/^"/, this.opts.quote)
.replace(/"$/, this.opts.quote);
Expand Down
11 changes: 11 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => {
const opts = ' --quote "\'"';

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

// Double Quote

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

testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => {
const opts = {
quote: '\''
};

const parser = new AsyncParser(opts);
parser.fromInput(jsonFixtures.doubleQuotes()).promise()
.then(csv => t.equal(csv, csvFixtures.doubleQuotesUnescaped))
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

// Double Quote

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

testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => {
const opts = {
quote: '\''
};

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

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

// Double Quote

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

testRunner.add('should not escape \'"\' when setting \'quote\' set to something else', (t) => {
const opts = {
quote: '\''
};

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

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

// Double Quote

testRunner.add('should escape quotes with double quotes', (t) => {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/csv/doubleQuotesUnescaped.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'a string'
'with a description'
'with a description and "quotes"'