Skip to content

Commit

Permalink
feat(json2csv): use sort function for sortHeader option (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
mebibou committed Jan 15, 2022
1 parent 023eec1 commit 5a8b7e0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -96,7 +96,7 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
* Note: Using this option may override other options, including `useDateIso8601Format` and `useLocaleFormat`.
* `prependHeader` - Boolean - Should the auto-generated header be prepended as the first line in the CSV?
* Default: `true`
* `sortHeader` - Boolean - Should the header keys be sorted in alphabetical order?
* `sortHeader` - Boolean or Function - Should the header keys be sorted in alphabetical order? or pass a function to use a custom sorting function
* Default: `false`
* `trimFieldValues` - Boolean - Should the field values be trimmed?
* Default: `false`
Expand Down
2 changes: 1 addition & 1 deletion lib/converter.d.ts
Expand Up @@ -85,7 +85,7 @@ export interface IFullOptions extends ISharedOptions {
* Should the header keys be sorted in alphabetical order
* @default false
*/
sortHeader?: boolean;
sortHeader?: boolean|((a: any, b: any) => number);

/**
* Should array values be "unwound" such that there is one line per value in the array?
Expand Down
2 changes: 1 addition & 1 deletion lib/json2csv.js
Expand Up @@ -103,7 +103,7 @@ const Json2Csv = function(options) {
*/
function sortHeaderFields(fieldNames) {
if (options.sortHeader) {
return fieldNames.sort();
return typeof options.sortHeader === 'function' ? fieldNames.sort(options.sortHeader) : fieldNames.sort();
}
return fieldNames;
}
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.js
Expand Up @@ -27,6 +27,7 @@ const fs = require('fs'),
{key: 'extraLine', file: '../data/csv/extraLine.csv'},
{key: 'noHeader', file: '../data/csv/noHeader.csv'},
{key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'},
{key: 'sortedHeaderCustom', file: '../data/csv/sortedHeaderCustom.csv'},
{key: 'emptyFieldValues', file: '../data/csv/emptyFieldValues.csv'},
{key: 'quotedEmptyFieldValue', file: '../data/csv/quotedEmptyFieldValue.csv'},
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'},
Expand Down
3 changes: 3 additions & 0 deletions test/data/csv/sortedHeaderCustom.csv
@@ -0,0 +1,3 @@
optionalField,object.subField,number,isBoolean,arrayOfStrings
this one has it,subValue,5,true,"[""test1"",""test2""]"
null,subValue,7,false,"[""test3"",""test4""]"
10 changes: 10 additions & 0 deletions test/json2csv.js
Expand Up @@ -328,6 +328,16 @@ function runTests(jsonTestData, csvTestData) {
});
});

it('should sort the header with a custom function in the csv', (done) => {
converter.json2csv(jsonTestData.assortedValues, (err, csv) => {
if (err) done(err);
csv.should.equal(csvTestData.sortedHeaderCustom);
done();
}, {
sortHeader: (a, b) => b.localeCompare(a)
});
});

it('should trim the header fields when specified', (done) => {
converter.json2csv(jsonTestData.trimHeader, (err, csv) => {
if (err) done(err);
Expand Down

0 comments on commit 5a8b7e0

Please sign in to comment.