Skip to content

Commit 002e75d

Browse files
authored
Merge pull request #250 from mrodrig/feat/support-for-207
Add arrayIndexesAsKeys option for #207
2 parents 6fccca0 + c3562d9 commit 002e75d

File tree

11 files changed

+63
-10
lines changed

11 files changed

+63
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.
5050

5151
* `array` - An array of JSON documents to be converted to CSV.
5252
* `options` - (Optional) A JSON document specifying any of the following key value pairs:
53+
* `arrayIndexesAsKeys` - Boolean - Should array indexes be included in the generated keys?
54+
* Default: `false`
55+
* 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.
5356
* `checkSchemaDifferences` - Boolean - Should all documents have the same schema?
5457
* Default: `false`
5558
* Note: An error will be thrown if some documents have differing schemas when this is set to `true`.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"cli"
4040
],
4141
"dependencies": {
42-
"deeks": "3.0.2",
43-
"doc-path": "4.0.2"
42+
"deeks": "3.1.0",
43+
"doc-path": "4.1.0"
4444
},
4545
"devDependencies": {
4646
"@types/mocha": "10.0.1",

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const errors = {
1818
};
1919

2020
export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
21+
arrayIndexesAsKeys: false,
2122
checkSchemaDifferences: false,
2223
delimiter : {
2324
field : ',',

src/json2csv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
1212
customValueParser = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : null,
1313
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
1414
deeksOptions = {
15+
arrayIndexesAsKeys: options.arrayIndexesAsKeys,
1516
expandNestedObjects: options.expandNestedObjects,
1617
expandArrayObjects: expandingWithoutUnwinding,
1718
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export interface Csv2JsonOptions extends Omit<SharedConverterOptions, 'keys'> {
8787
}
8888

8989
export interface Json2CsvOptions extends SharedConverterOptions {
90+
/** Should array indexes be included in the generated keys?
91+
* @default false
92+
*/
93+
arrayIndexesAsKeys?: boolean;
94+
9095
/**
9196
* Should all documents have the same schema?
9297
* @default false

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+
]

0 commit comments

Comments
 (0)