Skip to content

Commit fff1747

Browse files
author
Aurore
committed
Add BOM character option
1 parent 1d32059 commit fff1747

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

Diff for: bin/json2csv.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ program
2828
.option('-L, --ldjson', 'Treat the input as Line-Delimited JSON.')
2929
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
3030
.option('-a, --include-empty-rows', 'Includes empty rows in the resulting CSV output.')
31+
.option('-b, --with-bom', 'Includes BOM character at the beginning of the csv.')
3132
.parse(process.argv);
3233

3334
function getFields(callback) {
@@ -112,7 +113,8 @@ getFields(function (err, fields) {
112113
quotes: program.quote,
113114
defaultValue: program.defaultValue,
114115
flatten: program.flatten,
115-
includeEmptyRows: program.includeEmptyRows
116+
includeEmptyRows: program.includeEmptyRows,
117+
withBOM: program.withBOM
116118
};
117119

118120
if (program.delimiter) {

Diff for: lib/json2csv.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var flatten = require('flat');
2727
* @property {String[]} [unwindPath] - similar to MongoDB's $unwind, Deconstructs an array field from the input JSON to output a row for each element
2828
* @property {Boolean} [excelStrings] - converts string data into normalized Excel style data
2929
* @property {Boolean} [includeEmptyRows=false] - includes empty rows
30+
* @property {Boolean} [withBOM=false] - includes BOM character at the beginning of the csv
3031
*/
3132

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

142+
//#check with BOM, defaults to false
143+
params.withBOM = params.withBOM || false;
144+
141145
//#check unwindPath, defaults to empty array
142146
params.unwindPath = params.unwindPath || [];
143147

@@ -282,13 +286,17 @@ function createColumnContent(params, str) {
282286
}
283287
}
284288
});
289+
// Add BOM character if required
290+
if (params.withBOM) {
291+
str = '\ufeff ' + str;
292+
}
285293

286294
return str;
287295
}
288296

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

315323
/**
316324
* Performs the unwind logic if necessary to convert single JSON document into multiple rows
317-
*
325+
*
318326
* @param {Array} inputRows Array contaning single or multiple rows to unwind
319327
* @param {String} unwindPath Single path to do unwind
320328
* @returns {Array} Array of rows processed

Diff for: test/fixtures/csv/withBOM.csv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"carModel","price","color","transmission"
2+
"Audi",0,"blue",
3+
"BMW",15000,"red","manual"
4+
"Mercedes",20000,"yellow",
5+
"Citroën",30000,"green",

Diff for: test/fixtures/json/specialCharacters.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"carModel": "Audi",
4+
"price": 0,
5+
"color": "blue"
6+
},
7+
{
8+
"carModel": "BMW",
9+
"price": 15000,
10+
"color": "red",
11+
"transmission": "manual"
12+
},
13+
{
14+
"carModel": "Mercedes",
15+
"price": 20000,
16+
"color": "yellow"
17+
},
18+
{
19+
"carModel": "Citroën",
20+
"price": 30000,
21+
"color": "green"
22+
}
23+
]

Diff for: test/helpers/load-fixtures.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var fixtures = [
2929
'emptyRowNotIncluded',
3030
'emptyRowDefaultValues',
3131
'unwind',
32-
'unwind2'
32+
'unwind2',
33+
'withBOM',
3334
];
3435

3536
/*eslint-disable no-console*/

Diff for: test/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var jsonEmptyRow = require('./fixtures/json/emptyRow');
1616
var jsonUnwind = require('./fixtures/json/unwind');
1717
var jsonUnwind2 = require('./fixtures/json/unwind2');
1818
var jsonNewLine = require('./fixtures/json/newLine');
19+
var jsonSpecialCharacters = require('./fixtures/json/specialCharacters');
1920
var csvFixtures = {};
2021

2122
async.parallel(loadFixtures(csvFixtures), function (err) {
@@ -620,7 +621,6 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
620621
})
621622
});
622623

623-
624624
test('should unwind twice an array into multiple rows', function(t) {
625625
json2csv({
626626
data: jsonUnwind2,
@@ -665,4 +665,13 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
665665
t.end();
666666
});
667667
});
668+
669+
test('should add BOM character', function (t) {
670+
var csv = json2csv({
671+
data: jsonSpecialCharacters,
672+
withBOM: true
673+
});
674+
t.equal(csv.length, csvFixtures.withBOM.length);
675+
t.end();
676+
});
668677
});

0 commit comments

Comments
 (0)