Skip to content

Commit f9905f8

Browse files
committed
test: add additional test case for key appearing later in key path
For #244
1 parent d0ff411 commit f9905f8

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const csvFileConfig = [
5050
{key: 'falsyValues', file: '../data/csv/falsyValues.csv'},
5151
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
5252
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
53+
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
5354
];
5455

5556
function readCsvFile(filePath: string) {

test/config/testJsonFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ export default {
4343
quotedFieldWithNewline: require('../data/json/quotedFieldWithNewline.json'),
4444
falsyValues: require('../data/json/falsyValues.json'),
4545
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
46+
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
4647
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,name.arr
2+
1,this should appear
3+
2,this should also appear
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"id": 1,
4+
"arr": [
5+
{
6+
"name": "foo"
7+
}
8+
],
9+
"name": {
10+
"arr": "this should appear"
11+
}
12+
},
13+
{
14+
"id": 2,
15+
"arr": [
16+
{
17+
"name": "bar"
18+
}
19+
],
20+
"name": {
21+
"arr": "this should also appear"
22+
}
23+
}
24+
]

test/json2csv.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ export function runTests() {
513513
assert.equal(csv, updatedCsv);
514514
});
515515

516+
// Test case for #244
517+
it('should exclude a matched key prefix, but not if it is not at the start of the key path', () => {
518+
const csv = json2csv(jsonTestData.excludeKeyPattern, {
519+
expandArrayObjects: true,
520+
excludeKeys: ['arr']
521+
});
522+
523+
assert.equal(csv, csvTestData.excludeKeyPattern);
524+
});
525+
516526
it('should use a custom value parser function when provided', () => {
517527
const updatedCsv = csvTestData.trimmedFields.split('\n');
518528
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';

0 commit comments

Comments
 (0)