Skip to content

Commit

Permalink
feat: Add BOM character option (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroreM authored and knownasilya committed Jul 18, 2017
1 parent 1d32059 commit 0c799ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
4 changes: 3 additions & 1 deletion bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ program
.option('-L, --ldjson', 'Treat the input as Line-Delimited JSON.')
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
.option('-a, --include-empty-rows', 'Includes empty rows in the resulting CSV output.')
.option('-b, --with-bom', 'Includes BOM character at the beginning of the csv.')
.parse(process.argv);

function getFields(callback) {
Expand Down Expand Up @@ -112,7 +113,8 @@ getFields(function (err, fields) {
quotes: program.quote,
defaultValue: program.defaultValue,
flatten: program.flatten,
includeEmptyRows: program.includeEmptyRows
includeEmptyRows: program.includeEmptyRows,
withBOM: program.withBOM
};

if (program.delimiter) {
Expand Down
12 changes: 10 additions & 2 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var flatten = require('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
* @property {Boolean} [includeEmptyRows=false] - includes empty rows
* @property {Boolean} [withBOM=false] - includes BOM character at the beginning of the csv
*/

/**
Expand Down Expand Up @@ -138,6 +139,9 @@ function checkParams(params) {
//#check include empty rows, defaults to false
params.includeEmptyRows = params.includeEmptyRows || false;

//#check with BOM, defaults to false
params.withBOM = params.withBOM || false;

//#check unwindPath, defaults to empty array
params.unwindPath = params.unwindPath || [];

Expand Down Expand Up @@ -282,13 +286,17 @@ function createColumnContent(params, str) {
}
}
});
// Add BOM character if required
if (params.withBOM) {
str = '\ufeff ' + str;
}

return str;
}

/**
* Performs the unwind recursively in specified sequence
*
*
* @param {Array} originalData The params.data value. Original array of JSON objects
* @param {String[]} unwindPaths The params.unwindPath value. Unwind strings to be used to deconstruct array
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
Expand All @@ -314,7 +322,7 @@ function createDataRows(originalData, unwindPaths) {

/**
* Performs the unwind logic if necessary to convert single JSON document into multiple rows
*
*
* @param {Array} inputRows Array contaning single or multiple rows to unwind
* @param {String} unwindPath Single path to do unwind
* @returns {Array} Array of rows processed
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/csv/withBOM.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"carModel","price","color","transmission"
"Audi",0,"blue",
"BMW",15000,"red","manual"
"Mercedes",20000,"yellow",
"Citroën",30000,"green",
23 changes: 23 additions & 0 deletions test/fixtures/json/specialCharacters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"carModel": "Audi",
"price": 0,
"color": "blue"
},
{
"carModel": "BMW",
"price": 15000,
"color": "red",
"transmission": "manual"
},
{
"carModel": "Mercedes",
"price": 20000,
"color": "yellow"
},
{
"carModel": "Citroën",
"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 @@ -29,7 +29,8 @@ var fixtures = [
'emptyRowNotIncluded',
'emptyRowDefaultValues',
'unwind',
'unwind2'
'unwind2',
'withBOM',
];

/*eslint-disable no-console*/
Expand Down
13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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 jsonSpecialCharacters = require('./fixtures/json/specialCharacters');
var csvFixtures = {};

async.parallel(loadFixtures(csvFixtures), function (err) {
Expand Down Expand Up @@ -620,7 +621,6 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
})
});


test('should unwind twice an array into multiple rows', function(t) {
json2csv({
data: jsonUnwind2,
Expand Down Expand Up @@ -665,4 +665,15 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
t.end();
});
});

test('should add BOM character', function (t) {
var csv = json2csv({
data: jsonSpecialCharacters,
withBOM: true
});
// Compare csv length to check if the BOM character and the space after are present
t.equal(csv.length, csvFixtures.default.length + 2);
t.equal(csv.length, csvFixtures.withBOM.length);
t.end();
});
});

0 comments on commit 0c799ca

Please sign in to comment.