Skip to content

Commit

Permalink
Merge pull request #264 from Altioc/main
Browse files Browse the repository at this point in the history
Fix empty array literals being added when an empty array is provided and keys option is provided
  • Loading branch information
mrodrig authored Sep 26, 2024
2 parents ae91034 + c5b0fdf commit dd1514d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"name": "json-2-csv",
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "5.5.5",
"version": "5.5.6",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
11 changes: 8 additions & 3 deletions src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
* list of field names.
*/
function processSchemas(documentSchemas: string[][]) {
// If there are no document schemas then there is nothing to diff and no unqiue fields to get
if (documentSchemas.length === 0) {
return [];
}

// If the user wants to check for the same schema (regardless of schema ordering)
if (options.checkSchemaDifferences) {
return checkSchemaDifferences(documentSchemas);
Expand All @@ -53,8 +58,8 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
function checkSchemaDifferences(documentSchemas: string[][]) {
// have multiple documents - ensure only one schema (regardless of field ordering)
const firstDocSchema = documentSchemas[0],
restOfDocumentSchemas = documentSchemas.slice(1),
schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas);
restOfDocumentSchemas = documentSchemas.slice(1),

Check failure on line 61 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Expected indentation of 12 spaces but found 8

Check failure on line 61 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Expected indentation of 12 spaces but found 8

Check failure on line 61 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22)

Expected indentation of 12 spaces but found 8
schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas);

Check failure on line 62 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Expected indentation of 12 spaces but found 8

Check failure on line 62 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Expected indentation of 12 spaces but found 8

Check failure on line 62 in src/json2csv.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 22)

Expected indentation of 12 spaces but found 8

// If there are schema inconsistencies, throw a schema not the same error
if (schemaDifferences) {
Expand Down Expand Up @@ -457,7 +462,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
*/
function convert(data: object[]) {
// Single document, not an array
if (utils.isObject(data) && !data.length) {
if (!Array.isArray(data)) {
data = [data]; // Convert to an array of the given document
}

Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const csvFileConfig = [
{key: 'trimmedHeader', file: '../data/csv/trimmedHeader.csv'},
{key: 'excelBOM', file: '../data/csv/excelBOM.csv'},
{key: 'specifiedKeys', file: '../data/csv/specifiedKeys.csv'},
{key: 'specifiedKeysNoData', file: '../data/csv/specifiedKeysNoData.csv'},
{key: 'extraLine', file: '../data/csv/extraLine.csv'},
{key: 'noHeader', file: '../data/csv/noHeader.csv'},
{key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'},
Expand Down
1 change: 1 addition & 0 deletions test/data/csv/specifiedKeysNoData.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arrayOfStrings,object.subField
7 changes: 7 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ export function runTests() {
assert.equal(csv, csvTestData.specifiedKeys);
});

it('should only contain a header when given an empty array and the keys option is provided', () => {
const csv = json2csv(jsonTestData.noData, {
keys: ['arrayOfStrings', 'object.subField']
});
assert.equal(csv, csvTestData.specifiedKeysNoData);
});

it('should use the specified empty field value, if provided', () => {
jsonTestData.emptyFieldValues[0].number = undefined;

Expand Down

0 comments on commit dd1514d

Please sign in to comment.