Skip to content

Commit dd1514d

Browse files
authored
Merge pull request #264 from Altioc/main
Fix empty array literals being added when an empty array is provided and keys option is provided
2 parents ae91034 + c5b0fdf commit dd1514d

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "5.5.5",
8+
"version": "5.5.6",
99
"homepage": "https://mrodrig.github.io/json-2-csv",
1010
"repository": {
1111
"type": "git",

src/json2csv.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
3535
* list of field names.
3636
*/
3737
function processSchemas(documentSchemas: string[][]) {
38+
// If there are no document schemas then there is nothing to diff and no unqiue fields to get
39+
if (documentSchemas.length === 0) {
40+
return [];
41+
}
42+
3843
// If the user wants to check for the same schema (regardless of schema ordering)
3944
if (options.checkSchemaDifferences) {
4045
return checkSchemaDifferences(documentSchemas);
@@ -53,8 +58,8 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
5358
function checkSchemaDifferences(documentSchemas: string[][]) {
5459
// have multiple documents - ensure only one schema (regardless of field ordering)
5560
const firstDocSchema = documentSchemas[0],
56-
restOfDocumentSchemas = documentSchemas.slice(1),
57-
schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas);
61+
restOfDocumentSchemas = documentSchemas.slice(1),
62+
schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas);
5863

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

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const csvFileConfig = [
2525
{key: 'trimmedHeader', file: '../data/csv/trimmedHeader.csv'},
2626
{key: 'excelBOM', file: '../data/csv/excelBOM.csv'},
2727
{key: 'specifiedKeys', file: '../data/csv/specifiedKeys.csv'},
28+
{key: 'specifiedKeysNoData', file: '../data/csv/specifiedKeysNoData.csv'},
2829
{key: 'extraLine', file: '../data/csv/extraLine.csv'},
2930
{key: 'noHeader', file: '../data/csv/noHeader.csv'},
3031
{key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'},

test/data/csv/specifiedKeysNoData.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arrayOfStrings,object.subField

test/json2csv.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ export function runTests() {
293293
assert.equal(csv, csvTestData.specifiedKeys);
294294
});
295295

296+
it('should only contain a header when given an empty array and the keys option is provided', () => {
297+
const csv = json2csv(jsonTestData.noData, {
298+
keys: ['arrayOfStrings', 'object.subField']
299+
});
300+
assert.equal(csv, csvTestData.specifiedKeysNoData);
301+
});
302+
296303
it('should use the specified empty field value, if provided', () => {
297304
jsonTestData.emptyFieldValues[0].number = undefined;
298305

0 commit comments

Comments
 (0)