Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double quote escaping before new line #268

Merged
merged 1 commit into from
Mar 5, 2018
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
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming your tests pass, this fix is good with me. Although I thought $ would match both end of line and end of string. Or does it only match end of string?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like $ only matches line break if the multiline flag (m) is true (which it's not). So this looks good to me.

.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\\\\"}
]