Skip to content

Preserve new lines in cells with option preserveNewLinesInCells (#91) #171

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

Merged
merged 2 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo, "should be used with"

Copy link
Collaborator

Choose a reason for hiding this comment

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

"line endings"


## 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`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cells are Excell specific, let's call this something more generic.

- `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();
});
});
});