diff --git a/.gitattributes b/.gitattributes index 4304d08c..38cef04d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ * text=auto -test/fixtures/csv/newLine.csv text eol=crlf +test/fixtures/csv/eol.csv text eol=crlf diff --git a/README.md b/README.md index e0167b18..9b346411 100644 --- a/README.md +++ b/README.md @@ -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. @@ -409,8 +408,7 @@ Usage: json2csv [options] -f, --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 diff --git a/bin/json2csv.js b/bin/json2csv.js index aff2e22e..762623a2 100755 --- a/bin/json2csv.js +++ b/bin/json2csv.js @@ -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') @@ -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(','); } diff --git a/index.d.ts b/index.d.ts index 2f4420fe..9fc7fddc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,7 +26,6 @@ declare namespace json2csv { doubleQuote?: string; noHeader?: boolean; eol?: string; - newLine?: string; flatten?: boolean; unwindPath?: string | string[]; excelStrings?: boolean; diff --git a/lib/json2csv.js b/lib/json2csv.js index 51cdf39e..3cf044cd 100644 --- a/lib/json2csv.js +++ b/lib/json2csv.js @@ -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 @@ -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 : '"'; @@ -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; @@ -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; } } }); diff --git a/test/fixtures/csv/eol.csv b/test/fixtures/csv/eol.csv index e32db90c..bf2246de 100644 --- a/test/fixtures/csv/eol.csv +++ b/test/fixtures/csv/eol.csv @@ -1,5 +1,5 @@ "carModel","price","color" -"Audi",0,"blue"; -"BMW",15000,"red"; -"Mercedes",20000,"yellow"; -"Porsche",30000,"green"; \ No newline at end of file +"Audi",0,"blue" +"BMW",15000,"red" +"Mercedes",20000,"yellow" +"Porsche",30000,"green" \ No newline at end of file diff --git a/test/fixtures/csv/newLine.csv b/test/fixtures/csv/newLine.csv deleted file mode 100644 index bf2246de..00000000 --- a/test/fixtures/csv/newLine.csv +++ /dev/null @@ -1,5 +0,0 @@ -"carModel","price","color" -"Audi",0,"blue" -"BMW",15000,"red" -"Mercedes",20000,"yellow" -"Porsche",30000,"green" \ No newline at end of file diff --git a/test/fixtures/json/newLine.json b/test/fixtures/json/eol.json similarity index 100% rename from test/fixtures/json/newLine.json rename to test/fixtures/json/eol.json diff --git a/test/helpers/load-fixtures.js b/test/helpers/load-fixtures.js index 48ddc8fc..137e05d5 100644 --- a/test/helpers/load-fixtures.js +++ b/test/helpers/load-fixtures.js @@ -16,7 +16,6 @@ var fixtures = [ 'reversed', 'tsv', 'eol', - 'newLine', 'fieldNames', 'withSimpleQuotes', 'nested', diff --git a/test/index.js b/test/index.js index 7f6b2ed2..7abff115 100644 --- a/test/index.js +++ b/test/index.js @@ -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 = {}; @@ -264,7 +264,7 @@ 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); @@ -272,18 +272,6 @@ async.parallel(loadFixtures(csvFixtures), function (err) { }); }); - 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, @@ -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"\\"""'); @@ -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, [ @@ -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);