Skip to content

Commit fa991cf

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: double quote escaping before new line (#268)
1 parent e70aff1 commit fa991cf

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

lib/JSON2CSVBase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class JSON2CSVBase {
181181
//a backslash, and it's not at the end of the stringifiedValue.
182182
stringifiedValue = stringifiedValue
183183
.replace(/^"(.*)"$/, this.opts.quote + '$1' + this.opts.quote)
184-
.replace(/(\\")(?=.)/g, this.opts.doubleQuote)
184+
.replace(/(\\")(?!$)/g, this.opts.doubleQuote)
185185
.replace(/\\\\/g, '\\');
186186

187187
if (this.opts.excelStrings && typeof value === 'string') {

test/JSON2CSVParser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
425425
t.end();
426426
});
427427

428+
testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
429+
const opts = {
430+
fields: ['a string']
431+
};
432+
433+
const parser = new Json2csvParser(opts);
434+
const csv = parser.parse(jsonFixtures.backslashBeforeNewLine);
435+
436+
t.equal(csv, csvFixtures.backslashBeforeNewLine);
437+
t.end();
438+
});
439+
428440
// Delimiter
429441

430442
testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => {

test/JSON2CSVTransform.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,24 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
612612
.on('error', err => t.notOk(true, err.message));
613613
});
614614

615+
testRunner.add('should escape quotes before new line with value in \'doubleQuote\'', (t) => {
616+
const opts = {
617+
fields: ['a string']
618+
};
619+
620+
const transform = new Json2csvTransform(opts);
621+
const processor = jsonFixtures.backslashBeforeNewLine().pipe(transform);
622+
623+
let csv = '';
624+
processor
625+
.on('data', chunk => (csv += chunk.toString()))
626+
.on('end', () => {
627+
t.equal(csv, csvFixtures.backslashBeforeNewLine);
628+
t.end();
629+
})
630+
.on('error', err => t.notOk(true, err.message));
631+
});
632+
615633
// Delimiter
616634

617635
testRunner.add('should use a custom delimiter when \'delimiter\' property is defined', (t) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"a string"
2+
"with a description"
3+
"with a description and ""
4+
quotes and backslash\"
5+
"with a description and ""
6+
quotes and backslash\\"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"a string": "with a description"},
3+
{"a string": "with a description and \"\nquotes and backslash\\"},
4+
{"a string": "with a description and \"\nquotes and backslash\\\\"}
5+
]

0 commit comments

Comments
 (0)