Skip to content

Commit

Permalink
Fix/newlines in cells (#114)
Browse files Browse the repository at this point in the history
* add test for newline within cell content

* preserve newlines in cells
  • Loading branch information
dbuentello authored and Ilya Radchenko committed May 20, 2016
1 parent dbdc877 commit a8c63f4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ function createColumnContent(params, str) {
//JSON.stringify('\\') results in a string with two backslash
//characters in it. I.e. '\\\\'.
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\');


// preserve newlines
stringifiedElement = stringifiedElement.split('\\n').join('\n')

if (params.excelStrings && typeof val === 'string') {
stringifiedElement = '"="' + stringifiedElement + '""';
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/csv/newLineCellContent.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"carModel","price","color"
"Aud
i",0,"blue"
"BMW",15000,"red"
"Me
rcedes",20000,"yellow"
"Porsche",30000,"green"
6 changes: 6 additions & 0 deletions test/fixtures/json/newlines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{ "carModel": "Aud\ni", "price": 0, "color": "blue" },
{ "carModel": "BMW", "price": 15000, "color": "red" },
{ "carModel": "Me\nrcedes", "price": 20000, "color": "yellow" },
{ "carModel": "Porsche", "price": 30000, "color": "green" }
]
3 changes: 2 additions & 1 deletion test/helpers/load-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var fixtures = [
'flattenedEmbeddedJson',
'fancyfields',
'trailingBackslash',
'excelStrings'
'excelStrings',
'newLineCellContent'
];

/*eslint-disable no-console*/
Expand Down
17 changes: 14 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var jsonNested = require('./fixtures/json/nested');
var jsonDefaultValue = require('./fixtures/json/defaultValue');
var jsonDefaultValueEmpty = require('./fixtures/json/defaultValueEmpty');
var jsonTrailingBackslash = require('./fixtures/json/trailingBackslash');
var jsonNewlines = require('./fixtures/json/newlines');
var csvFixtures = {};

async.parallel(loadFixtures(csvFixtures), function (err) {
Expand Down Expand Up @@ -51,7 +52,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end();
});
});


test('should parse json to csv without fields', function (t) {
json2csv({
Expand Down Expand Up @@ -362,7 +363,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end();
});
});

test('should escape " when preceeded by \\', function (t){
json2csv({
data: [{field: '\\"'}]
Expand All @@ -372,7 +373,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end();
});
});

test('should format strings to force excel to view the values as strings', function (t) {
json2csv({
data: jsonDefault,
Expand All @@ -384,4 +385,14 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end();
});
});

test('should preserve newlines within cell content', function (t) {
json2csv({
data: jsonNewlines
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.newLineCellContent);
t.end();
});
});
});

0 comments on commit a8c63f4

Please sign in to comment.