Skip to content

Commit

Permalink
fix: double quote escaping before new line (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Mar 5, 2018
1 parent e70aff1 commit fa991cf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class JSON2CSVBase {
//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, this.opts.doubleQuote)
.replace(/\\\\/g, '\\');

if (this.opts.excelStrings && typeof value === 'string') {
Expand Down
12 changes: 12 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
const opts = {
fields: ['a string']
};

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

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

// Delimiter

testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => {
Expand Down
18 changes: 18 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,24 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
const opts = {
fields: ['a string']
};

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

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

// Delimiter

testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/csv/backslashBeforeNewLine.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"a string"
"with a description"
"with a description and ""
quotes and backslash\"
"with a description and ""
quotes and backslash\\"
5 changes: 5 additions & 0 deletions test/fixtures/json/backslashBeforeNewLine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"a string": "with a description"},
{"a string": "with a description and \"\nquotes and backslash\\"},
{"a string": "with a description and \"\nquotes and backslash\\\\"}
]

0 comments on commit fa991cf

Please sign in to comment.