Skip to content

Commit

Permalink
Merge c3562d9 into 6fccca0
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodrig committed Feb 25, 2024
2 parents 6fccca0 + c3562d9 commit dfa8646
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -50,6 +50,9 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.

* `array` - An array of JSON documents to be converted to CSV.
* `options` - (Optional) A JSON document specifying any of the following key value pairs:
* `arrayIndexesAsKeys` - Boolean - Should array indexes be included in the generated keys?
* Default: `false`
* Note: This provides a more accurate representation of the JSON in the returned CSV, but may be less human readable. See [#207](https://github.com/mrodrig/json-2-csv/issues/207) for more details.
* `checkSchemaDifferences` - Boolean - Should all documents have the same schema?
* Default: `false`
* Note: An error will be thrown if some documents have differing schemas when this is set to `true`.
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -39,8 +39,8 @@
"cli"
],
"dependencies": {
"deeks": "3.0.2",
"doc-path": "4.0.2"
"deeks": "3.1.0",
"doc-path": "4.1.0"
},
"devDependencies": {
"@types/mocha": "10.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Expand Up @@ -18,6 +18,7 @@ export const errors = {
};

export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
arrayIndexesAsKeys: false,
checkSchemaDifferences: false,
delimiter : {
field : ',',
Expand Down
1 change: 1 addition & 0 deletions src/json2csv.ts
Expand Up @@ -12,6 +12,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
customValueParser = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : null,
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
deeksOptions = {
arrayIndexesAsKeys: options.arrayIndexesAsKeys,
expandNestedObjects: options.expandNestedObjects,
expandArrayObjects: expandingWithoutUnwinding,
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -87,6 +87,11 @@ export interface Csv2JsonOptions extends Omit<SharedConverterOptions, 'keys'> {
}

export interface Json2CsvOptions extends SharedConverterOptions {
/** Should array indexes be included in the generated keys?
* @default false
*/
arrayIndexesAsKeys?: boolean;

/**
* Should all documents have the same schema?
* @default false
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
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
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
@@ -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
@@ -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
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 dfa8646

Please sign in to comment.