Skip to content

Commit

Permalink
fix: backslash value not escaped properly (#202) (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
nahzz authored and knownasilya committed Oct 19, 2017
1 parent 8fd7aaf commit 2cf50f1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function createColumnContent(params, str) {
}

if (typeof val === 'object') {
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
// In some cases (e.g. val is a Date), stringifiedElement is already a quoted string.
// Strip the leading and trailing quotes if so, so we don't end up double-quoting it
stringifiedElement = replaceQuotationMarks(stringifiedElement, '');

Expand All @@ -278,6 +278,10 @@ function createColumnContent(params, str) {
stringifiedElement = '"="' + stringifiedElement + '""';
}

//Replace single quotes with double quotes. Single quotes are preceeded by
//a backslash, and it's not at the end of the stringifiedElement.
stringifiedElement = stringifiedElement.replace(/(\\")(?=.)/g, params.doubleQuotes);

line += stringifiedElement;
}

Expand All @@ -286,13 +290,10 @@ function createColumnContent(params, str) {

//remove last delimeter by its length
line = line.substring(0, line.length - params.del.length);
//Replace single quotes with double quotes. Single quotes are preceeded by
//a backslash. Be careful not to remove backslash content from the string.
line = line.split('\\\\').map(function (portion) {
return portion.replace(/\\"/g, params.doubleQuotes);
}).join('\\\\');

//Remove the final excess backslashes from the stringified value.
line = line.replace(/\\\\/g, '\\');
line = line.replace(/\\\\/g,'\\');

//If header exists, add it, otherwise, print only content
if (str !== '') {
str += eol + line + params.eol;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/csv/backslashAtEnd.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"a string"
"with a description"
"with a description and ""quotes and backslash\"
"with a description and ""quotes and backslash\"
3 changes: 3 additions & 0 deletions test/fixtures/csv/backslashAtEndInMiddleColumn.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"uuid","title","id"
"xxxx-yyyy","$25 something $50 \","someId"
"xxxx-yyyy","$25 something $50 \","someId"
5 changes: 5 additions & 0 deletions test/fixtures/json/backslashAtEnd.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 \"quotes and backslash\\"},
{"a string": "with a description and \"quotes and backslash\\\\"}
]
4 changes: 4 additions & 0 deletions test/fixtures/json/backslashAtEndInMiddleColumn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"uuid": "xxxx-yyyy", "title": "$25 something $50 \\", "id":"someId"},
{"uuid": "xxxx-yyyy", "title": "$25 something $50 \\\\", "id":"someId"}
]
2 changes: 2 additions & 0 deletions test/helpers/load-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var fixtures = [
'withoutQuotes',
'withNotExistField',
'quotes',
'backslashAtEnd',
'backslashAtEndInMiddleColumn',
'date',
'selected',
'reversed',
Expand Down
28 changes: 27 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var parseLdJson = require('../lib/parse-ldjson');
var loadFixtures = require('./helpers/load-fixtures');
var jsonDefault = require('./fixtures/json/default');
var jsonQuotes = require('./fixtures/json/quotes');
var backslashAtEnd = require('./fixtures/json/backslashAtEnd');
var backslashAtEndInMiddleColumn = require('./fixtures/json/backslashAtEndInMiddleColumn');
var jsonNested = require('./fixtures/json/nested');
var jsonDefaultValue = require('./fixtures/json/defaultValue');
var jsonDefaultValueEmpty = require('./fixtures/json/defaultValueEmpty');
Expand Down Expand Up @@ -198,6 +200,30 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
});
});


test('should not escape quotes with double quotes, when there is a backslah in the end', function (t) {
json2csv({
data: backslashAtEnd,
fields: ['a string']
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.backslashAtEnd);
t.end();
});
});


test('should not escape quotes with double quotes, when there is a backslah in the end, and its not the last column', function (t) {
json2csv({
data: backslashAtEndInMiddleColumn,
fields: ['uuid','title','id']
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.backslashAtEndInMiddleColumn);
t.end();
});
});

test('should use a custom delimiter when \'quotes\' property is present', function (t) {
json2csv({
data: jsonDefault,
Expand Down Expand Up @@ -466,7 +492,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
newLine: '\n'
}, function (error, csv){
t.error(error);
t.equal(csv, '"field"\n"\\""');
t.equal(csv, '"field"\n"\\"""');
t.end();
});
});
Expand Down

4 comments on commit 2cf50f1

@nahzz
Copy link
Contributor Author

@nahzz nahzz commented on 2cf50f1 Oct 23, 2017

Choose a reason for hiding this comment

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

Hi , is it possible to publish a new release with this fix ? Our production depends on this fix . Thank you !

@knownasilya
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, will get it out today.

@knownasilya
Copy link
Collaborator

Choose a reason for hiding this comment

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

Published

@nahzz
Copy link
Contributor Author

@nahzz nahzz commented on 2cf50f1 Oct 23, 2017

Choose a reason for hiding this comment

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

Thanks a lot .

Please sign in to comment.