Skip to content

Commit

Permalink
Fix 203 (#204)
Browse files Browse the repository at this point in the history
* Handle case where no column headers are present.

As reported in #203, there are use cases where the CSV may not have
column headers for a particular set of data. In this case, the expected
output simply ignores these unnamed data values, but as of [email protected],
the output was an error indicating that no key path was provided. This
commit fixes #203 by capturing and handling that error from the doc-path
module.

Fixes #203

* chore(release): 3.14.2
  • Loading branch information
mrodrig authored Aug 7, 2021
1 parent 523cf3a commit d754925
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,13 @@ const Csv2Json = function(options) {
// If there is a value at the key's index in the line, set the value; otherwise null
let value = retrieveRecordValueFromLine(line, key);

// Otherwise add the key and value to the document
return path.setPath(document, key.value, value);
try {
// Otherwise add the key and value to the document
return path.setPath(document, key.value, value);
} catch (error) {
// Catch any errors where key paths are null or '' and continue
return document;
}
}, {});
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

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": "3.14.1",
"version": "3.14.2",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion test/config/testCsvFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const fs = require('fs'),
{key: 'nativeMapMethod', file: '../data/csv/nativeMapMethod.csv'},
{key: 'nestedDotKeys', file: '../data/csv/nestedDotKeys.csv'},
{key: 'nestedDotKeysWithArray', file: '../data/csv/nestedDotKeysWithArray.csv'},
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'}
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'},
{key: 'emptyColumns', file: '../data/csv/emptyColumns.csv'}
];

function readCsvFile(filePath) {
Expand Down
3 changes: 2 additions & 1 deletion test/config/testJsonFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ module.exports = {
nativeMapMethod: require('../data/json/nativeMapMethod.json'),
nestedDotKeys: require('../data/json/nestedDotKeys.json'),
nestedDotKeysWithArray: require('../data/json/nestedDotKeysWithArray.json'),
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json')
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json'),
emptyColumns: require('../data/json/emptyColumns.json')
};
8 changes: 8 additions & 0 deletions test/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ function runTests(jsonTestData, csvTestData) {
done();
});
});

it('should drop any values with empty column headers', (done) => {
converter.csv2json(csvTestData.emptyColumns, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.emptyColumns);
done();
});
});
});

describe('Error Handling', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/data/csv/emptyColumns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test,test2,,
data1,data2,blah,blah
data3,data4,blah2,blah2
4 changes: 4 additions & 0 deletions test/data/json/emptyColumns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "test": "data1", "test2": "data2" },
{ "test": "data3", "test2": "data4" }
]

0 comments on commit d754925

Please sign in to comment.