Skip to content

Commit c3562d9

Browse files
committed
test: add test for arrayIndexesAsKeys option for #207
1 parent b017dfb commit c3562d9

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const csvFileConfig = [
5252
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
5353
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
5454
{key: 'wildcardMatch', file: '../data/csv/wildcardMatch.csv'},
55+
{key: 'arrayIndexesAsKeys', file: '../data/csv/arrayIndexesAsKeys.csv'},
5556
];
5657

5758
function readCsvFile(filePath: string) {

test/config/testJsonFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ export default {
4545
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
4646
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
4747
wildcardMatch: require('../data/json/wildcardMatch.json'),
48+
arrayIndexesAsKeys: require('../data/json/arrayIndexesAsKeys.json'),
4849
};

test/data/csv/arrayIndexesAsKeys.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test.list.0.a,test.list.0.optionA,test.list.1.a,test.list.1.optionB
2+
1,ac,2,radio
3+
3,cd,4,heat
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[
2+
{
3+
"test": {
4+
"list": [
5+
{
6+
"a": 1,
7+
"optionA": "ac"
8+
},
9+
{
10+
"a": 2,
11+
"optionB": "radio"
12+
}
13+
]
14+
}
15+
},
16+
{
17+
"test": {
18+
"list": [
19+
{
20+
"a": 3,
21+
"optionA": "cd"
22+
},
23+
{
24+
"a": 4,
25+
"optionB": "heat"
26+
}
27+
]
28+
}
29+
}
30+
]

test/json2csv.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,14 @@ export function runTests() {
578578
assert.equal(csv, updatedCsv);
579579
});
580580

581+
// Test case for #207
582+
it('should include the array indexes in CSV key headers if specified via the option', () => {
583+
const csv = json2csv(jsonTestData.arrayIndexesAsKeys, {
584+
arrayIndexesAsKeys: true,
585+
});
586+
assert.equal(csv, csvTestData.arrayIndexesAsKeys);
587+
});
588+
581589
it('should use a custom value parser function when provided', () => {
582590
const updatedCsv = csvTestData.trimmedFields.split('\n');
583591
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';

0 commit comments

Comments
 (0)