Skip to content

Commit

Permalink
test: add test for arrayIndexesAsKeys option for #207
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodrig committed Feb 25, 2024
1 parent b017dfb commit c3562d9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const csvFileConfig = [
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
{key: 'wildcardMatch', file: '../data/csv/wildcardMatch.csv'},
{key: 'arrayIndexesAsKeys', file: '../data/csv/arrayIndexesAsKeys.csv'},
];

function readCsvFile(filePath: string) {
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export default {
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
wildcardMatch: require('../data/json/wildcardMatch.json'),
arrayIndexesAsKeys: require('../data/json/arrayIndexesAsKeys.json'),
};
3 changes: 3 additions & 0 deletions test/data/csv/arrayIndexesAsKeys.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.list.0.a,test.list.0.optionA,test.list.1.a,test.list.1.optionB
1,ac,2,radio
3,cd,4,heat
30 changes: 30 additions & 0 deletions test/data/json/arrayIndexesAsKeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"test": {
"list": [
{
"a": 1,
"optionA": "ac"
},
{
"a": 2,
"optionB": "radio"
}
]
}
},
{
"test": {
"list": [
{
"a": 3,
"optionA": "cd"
},
{
"a": 4,
"optionB": "heat"
}
]
}
}
]
8 changes: 8 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ export function runTests() {
assert.equal(csv, updatedCsv);
});

// Test case for #207
it('should include the array indexes in CSV key headers if specified via the option', () => {
const csv = json2csv(jsonTestData.arrayIndexesAsKeys, {
arrayIndexesAsKeys: true,
});
assert.equal(csv, csvTestData.arrayIndexesAsKeys);
});

it('should use a custom value parser function when provided', () => {
const updatedCsv = csvTestData.trimmedFields.split('\n');
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';
Expand Down

0 comments on commit c3562d9

Please sign in to comment.