Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty array literals being added when an empty array is provided and keys option is provided #264

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
* 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 @@
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
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

// If there are schema inconsistencies, throw a schema not the same error
if (schemaDifferences) {
Expand Down Expand Up @@ -457,7 +462,7 @@
*/
function convert(data: object[]) {
// Single document, not an array
if (utils.isObject(data) && !data.length) {
Copy link
Contributor Author

@Altioc Altioc Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All arrays are typed as object's and data.length of an empty array is 0 and !0 is true which is the source of the bug.

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
Loading