Skip to content

Commit 3151141

Browse files
committed
Merge pull request #32 from litixsoft/optional_table_headers_sorting
add option `SORT_HEADER` that allows to sort the auto generated header
2 parents 745f7c5 + c4fbc49 commit 3151141

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var converter = require('json-2-csv');
3636
* `WRAP` - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes). Default: `''`
3737
* `CHECK_SCHEMA_DIFFERENCES` - Boolean - Should we require all documents to have the same schema? Default: `true`
3838
* `PREPEND_HEADER` - Boolean - Should the auto-generated header be prepended as the first line in the CSV? Default: `true`
39+
* `SORT_HEADER` - Boolean - Should the auto-generated header be sorted? Default: `false`
3940
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
4041
* `KEYS` - Array - Specify the keys (as strings) that should be converted. Default: `null`
4142
* If you have a nested object (ie. {info : {name: 'Mike'}}), then set options.KEYS to ['info.name']

lib/constants.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"EOL" : "\n",
2929
"PREPEND_HEADER" : true,
30+
"SORT_HEADER" : false,
3031
"PARSE_CSV_NUMBERS" : false,
3132
"KEYS" : null,
3233
"CHECK_SCHEMA_DIFFERENCES": true

lib/json-2-csv.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ var generateHeading = function(data) {
4747
return Promise.reject(new Error(constants.Errors.json2csv.notSameSchema));
4848
}
4949

50-
return Promise.resolve(_.flatten(keys[0]));
50+
51+
var result = _.flatten(keys[0]);
52+
53+
if (options.SORT_HEADER) {
54+
result.sort();
55+
}
56+
57+
return Promise.resolve(result);
5158
}
5259

5360
var uniqueKeys = [];
@@ -57,6 +64,10 @@ var generateHeading = function(data) {
5764
uniqueKeys = _.union(uniqueKeys, _.flatten(keyList));
5865
});
5966

67+
if (options.SORT_HEADER) {
68+
uniqueKeys.sort();
69+
}
70+
6071
return Promise.resolve(uniqueKeys);
6172
};
6273

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
carModel,color,price
2+
Audi,blue,10000
3+
BMW,red,15000
4+
Mercedes,yellow,20000
5+
Porsche,green,30000

test/testCsvFilesList.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{"key": "nestedQuotes", "file": "test/CSV/unQuoted/nestedQuotes.csv"},
1010
{"key": "noData", "file": "test/CSV/unQuoted/noData.csv"},
1111
{"key": "regularJson", "file": "test/CSV/unQuoted/regularJson.csv"},
12+
{"key": "regularJsonSorted", "file": "test/CSV/unQuoted/regularJsonSorted.csv"},
1213
{"key": "singleDoc", "file": "test/CSV/unQuoted/singleDoc.csv"},
1314
{"key": "differentSchemas", "file": "test/CSV/unQuoted/differentSchemas.csv"}
1415
]

test/testJson2Csv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ var json2csvTests = function () {
2626
});
2727
});
2828

29+
it('should convert plain JSON to CSV and sort the header', function(done) {
30+
converter.json2csv(jsonTestData.regularJson, function(err, csv) {
31+
if (err) { throw err; }
32+
true.should.equal(_.isEqual(err, null));
33+
csv.should.equal(csvTestData.unQuoted.regularJsonSorted);
34+
csv.split(options.EOL).length.should.equal(6);
35+
done();
36+
}, {SORT_HEADER: true});
37+
});
38+
2939
it('should parse nested JSON to CSV - 1', function(done) {
3040
converter.json2csv(jsonTestData.nestedJson, function(err, csv) {
3141
if (err) { throw err; }

0 commit comments

Comments
 (0)