Skip to content

Commit

Permalink
fix: Combine EOL and newLine parameters (#219)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: newLine removed, eol kept.
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 20, 2018
1 parent 5e402dc commit 4668a8b
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* text=auto
test/fixtures/csv/newLine.csv text eol=crlf
test/fixtures/csv/eol.csv text eol=crlf
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ try {
- `quote` - String, quote around cell values and column names. Defaults to `"` if not specified.
- `doubleQuote` - String, the value to replace double quote in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
- `noHeader` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
- `eol` - String, it gets added to each row of data. Defaults to `` if not specified.
- `newLine` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
- `eol` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
- `unwindPath` - Array of Strings, creates multiple rows from a single JSON document similar to MongoDB's $unwind
- `excelStrings` - Boolean, converts string data into normalized Excel style data.
Expand Down Expand Up @@ -409,8 +408,7 @@ Usage: json2csv [options]
-f, --fields <fields> Specify the fields to convert.
-l, --fieldList [list] Specify a file with a list of fields to include. One field per line.
-d, --delimiter [delimiter] Specify a delimiter other than the default comma to use.
-e, --eol [value] Specify an EOL value after each row.
-z, --newLine [value] Specify an new line value for separating rows.
-e, --eol [value] Specify an End-of-Line value for separating rows.
-q, --quote [value] Specify an alternate quote value.
-n, --no-header Disable the column name header
-F, --flatten Flatten nested objects
Expand Down
7 changes: 1 addition & 6 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ program
.option('-l, --fieldList [list]', 'Specify a file with a list of fields to include. One field per line.')
.option('-d, --delimiter [delimiter]', 'Specify a delimiter other than the default comma to use.')
.option('-v, --defaultValue [defaultValue]', 'Specify a default value other than empty string.')
.option('-e, --eol [value]', 'Specify an EOL value after each row.')
.option('-z, --newLine [value]', 'Specify an new line value for separating rows.')
.option('-e, --eol [value]', 'Specify an End-of-Line value for separating rows.')
.option('-q, --quote [value]', 'Specify an alternate quote value.')
.option('-dq, --doubleQuotes [value]', 'Specify a value to replace double quote in strings')
.option('-n, --no-header', 'Disable the column name header')
Expand Down Expand Up @@ -127,10 +126,6 @@ getFields(function (err, fields) {
opts.eol = program.eol;
}

if (program.newLine) {
opts.newLine = program.newLine;
}

if (program.unwindPath) {
opts.unwindPath = program.unwindPath.split(',');
}
Expand Down
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ declare namespace json2csv {
doubleQuote?: string;
noHeader?: boolean;
eol?: string;
newLine?: string;
flatten?: boolean;
unwindPath?: string | string[];
excelStrings?: boolean;
Expand Down
10 changes: 4 additions & 6 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var flatten = require('flat');
* @property {String} [quote='"'] - quote around cell values and column names
* @property {String} [doubleQuote='""'] - the value to replace double quote in strings
* @property {Boolean} [noHeader=true] - determines whether or not CSV file will contain a title column
* @property {String} [eol=''] - it gets added to each row of data
* @property {String} [newLine] - overrides the default OS line ending (\n on Unix \r\n on Windows)
* @property {String} [eol=''] - overrides the default OS line ending (\n on Unix \r\n on Windows)
* @property {Boolean} [flatten=false] - flattens nested JSON using flat (https://www.npmjs.com/package/flat)
* @property {String[]} [unwindPath] - similar to MongoDB's $unwind, Deconstructs an array field from the input JSON to output a row for each element
* @property {Boolean} [excelStrings] - converts string data into normalized Excel style data
Expand Down Expand Up @@ -122,7 +121,7 @@ function checkParams(params) {
params.delimiter = params.delimiter || ',';

//#check end of line character
params.eol = params.eol || '';
params.eol = params.eol || os.EOL || '\n';

//#check quotation mark
params.quote = typeof params.quote === 'string' ? params.quote : '"';
Expand Down Expand Up @@ -210,7 +209,6 @@ function createColumnContent(params, str) {
//if null do nothing, if empty object without includeEmptyRows do nothing
if (dataElement && (Object.getOwnPropertyNames(dataElement).length > 0 || params.includeEmptyRows)) {
var line = '';
var eol = params.newLine || os.EOL || '\n';

params.fields.forEach(function (fieldElement) {
var val;
Expand Down Expand Up @@ -296,9 +294,9 @@ function createColumnContent(params, str) {

//If header exists, add it, otherwise, print only content
if (str !== '') {
str += eol + line + params.eol;
str += params.eol + line;
} else {
str = line + params.eol;
str = line;
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/csv/eol.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"carModel","price","color"
"Audi",0,"blue";
"BMW",15000,"red";
"Mercedes",20000,"yellow";
"Porsche",30000,"green";
"Audi",0,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"
5 changes: 0 additions & 5 deletions test/fixtures/csv/newLine.csv

This file was deleted.

File renamed without changes.
1 change: 0 additions & 1 deletion test/helpers/load-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var fixtures = [
'reversed',
'tsv',
'eol',
'newLine',
'fieldNames',
'withSimpleQuotes',
'nested',
Expand Down
26 changes: 7 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var jsonOverriddenDefaultValue = require('./fixtures/json/overridenDefaultValue'
var jsonEmptyRow = require('./fixtures/json/emptyRow');
var jsonUnwind = require('./fixtures/json/unwind');
var jsonUnwind2 = require('./fixtures/json/unwind2');
var jsonNewLine = require('./fixtures/json/newLine');
var jsonEOL = require('./fixtures/json/eol');
var jsonSpecialCharacters = require('./fixtures/json/specialCharacters');
var csvFixtures = {};

Expand Down Expand Up @@ -264,26 +264,14 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price', 'color'],
eol: ';'
eol: '\r\n'
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.eol);
t.end();
});
});

test('should use a custom eol character when \'newLine\' property is present', function (t) {
json2csv({
data: jsonDefault,
fields: ['carModel', 'price', 'color'],
newLine: '\r\n'
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.newLine);
t.end();
});
});

test('should name columns as specified in \'fieldNames\' property', function (t) {
json2csv({
data: jsonDefault,
Expand Down Expand Up @@ -489,7 +477,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
test('should escape " when preceeded by \\', function (t){
json2csv({
data: [{field: '\\"'}],
newLine: '\n'
eol: '\n'
}, function (error, csv){
t.error(error);
t.equal(csv, '"field"\n"\\"""');
Expand Down Expand Up @@ -712,9 +700,9 @@ async.parallel(loadFixtures(csvFixtures), function (err) {

test('should not preserve new lines in values by default', function(t) {
json2csv({
data: jsonNewLine,
data: jsonEOL,
fields: ['a string'],
newLine: '\r\n',
eol: '\r\n',
}, function(error, csv) {
t.error(error);
t.equal(csv, [
Expand All @@ -728,9 +716,9 @@ async.parallel(loadFixtures(csvFixtures), function (err) {

test('should preserve new lines in values when options.preserveNewLinesInValues is true', function(t) {
json2csv({
data: jsonNewLine,
data: jsonEOL,
fields: ['a string'],
newLine: '\r\n',
eol: '\r\n',
preserveNewLinesInValues: true,
}, function(error, csv) {
t.error(error);
Expand Down

0 comments on commit 4668a8b

Please sign in to comment.