Skip to content

Commit

Permalink
Preserve new lines in cells with option preserveNewLinesInCells (#91)
Browse files Browse the repository at this point in the history
\n and \r are converted to \u2028 and \u2029 in order to pass through
JSON.stringify, then converted back to \n and \r.
Note any original \u2028 and \u2029 are converted to \n and \r.
  • Loading branch information
Clement Teule committed Apr 25, 2017
1 parent 1102a50 commit dd344c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ try {
- Supports optional custom quotation marks
- Not create CSV column title by passing hasCSVColumnTitle: false, into params.
- If field is not exist in object then the field value in CSV will be empty.
- Preserve new lines in cells. Should be used we \r\n line ending for full compatibility with Excel.

## Use as a module

Expand All @@ -67,6 +68,7 @@ try {
- `unwindPath` - String, creates multiple rows from a single JSON document similar to MongoDB's $unwind
- `excelStrings` - Boolean, converts string data into normalized Excel style data.
- `includeEmptyRows` - Boolean, includes empty rows. Defaults to `false`.
- `preserveNewLinesInCells` - Boolean, preserve \r and \n in cells. Defaults to `false`.
- `callback` - `function (error, csvString) {}`. If provided, will callback asynchronously. Only supported for compatibility reasons.

#### Example `fields` option
Expand Down
12 changes: 12 additions & 0 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,20 @@ function createColumnContent(params, str) {
}

if (val !== undefined) {
if (params.preserveNewLinesInCells && typeof val === 'string') {
val = val
.replace(/\n/g, '\u2028')
.replace(/\r/g, '\u2029');
}

var stringifiedElement = JSON.stringify(val);

if (params.preserveNewLinesInCells && typeof val === 'string') {
stringifiedElement = stringifiedElement
.replace(/\u2028/g, '\n')
.replace(/\u2029/g, '\r');
}

if (typeof val === 'object') stringifiedElement = JSON.stringify(stringifiedElement);

if (params.quotes !== '"') {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/json/newLine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{"a string": "with a \u2028description\\n and\na new line"},
{"a string": "with a \u2029\u2028description and\r\nanother new line"}
]
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var jsonTrailingBackslash = require('./fixtures/json/trailingBackslash');
var jsonOverriddenDefaultValue = require('./fixtures/json/overridenDefaultValue');
var jsonEmptyRow = require('./fixtures/json/emptyRow');
var jsonUnwind = require('./fixtures/json/unwind');
var jsonNewLine = require('./fixtures/json/newLine');
var csvFixtures = {};

async.parallel(loadFixtures(csvFixtures), function (err) {
Expand Down Expand Up @@ -616,4 +617,37 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end()
})
});

test('should not preserve new lines in cells by default', function(t) {
json2csv({
data: jsonNewLine,
fields: ['a string'],
newLine: '\r\n',
}, function(error, csv) {
t.error(error);
t.equal(csv, [
'"a string"',
'"with a \u2028description\\n and\\na new line"',
'"with a \u2029\u2028description and\\r\\nanother new line"'
].join('\r\n'));
t.end();
});
});

test('should preserve new lines in cells when options.preserveNewLinesInCell is true', function(t) {
json2csv({
data: jsonNewLine,
fields: ['a string'],
newLine: '\r\n',
preserveNewLinesInCells: true,
}, function(error, csv) {
t.error(error);
t.equal(csv, [
'"a string"',
'"with a \ndescription\\n and\na new line"',
'"with a \r\ndescription and\r\nanother new line"'
].join('\r\n'));
t.end();
});
});
});

0 comments on commit dd344c5

Please sign in to comment.