Skip to content

Commit

Permalink
Validate field titles against Object.keys list (#186)
Browse files Browse the repository at this point in the history
* Validate field titles against Object.keys list

As reported in #185, there was a bug where header values could not have
the same name as a Map method. This is because the code was naively
checking for the existence of a possible title field by simply accessing
the raw value of the Map. This resulted in Map.prototype.set being
returned when the "set" JSON key name was used. The logic has been
updated to check for header titles against an Object.keys list now since
that will not include prototype methods for the underlying Map object.

Fixes #185

* Add test for #185.

* Add Node 16,  remove Node 10 for travis test cfg.
  • Loading branch information
mrodrig committed May 20, 2021
1 parent 72688ef commit e72fa24
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ env:
- CC_TEST_REPORTER_ID=90382d7264499c819f742709fe7cb4b2d2a49cc90fd98a9c8414426ac4860e62
language: node_js
node_js:
- "16"
- "15"
- "14"
- "12"
- "10"
sudo: false
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
Expand Down
5 changes: 4 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,12 @@ const Json2Csv = function(options) {
* @returns {*}
*/
function generateCsvHeader(params) {
// #185 - generate a keys list to avoid finding native Map() methods
let fieldTitleMapKeys = Object.keys(options.fieldTitleMap);

params.header = params.headerFields
.map(function(field) {
const headerKey = options.fieldTitleMap[field] ? options.fieldTitleMap[field] : field;
const headerKey = fieldTitleMapKeys.includes(field) ? options.fieldTitleMap[field] : field;
return wrapFieldValueIfNecessary(headerKey);
})
.join(options.delimiter.field);
Expand Down
3 changes: 2 additions & 1 deletion test/config/testCsvFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const fs = require('fs'),
{key: 'firstColumnWrapCRLF', file: '../data/csv/firstColumnWrapCRLF.csv'},
{key: 'emptyLastFieldValue', file: '../data/csv/emptyLastFieldValue.csv'},
{key: 'emptyLastFieldValueNoEol', file: '../data/csv/emptyLastFieldValueNoEol.csv'},
{key: 'lastCharFieldDelimiter', file: '../data/csv/lastCharFieldDelimiter.csv'}
{key: 'lastCharFieldDelimiter', file: '../data/csv/lastCharFieldDelimiter.csv'},
{key: 'nativeMapMethod', file: '../data/csv/nativeMapMethod.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 @@ -29,5 +29,6 @@ module.exports = {
invalidParsedValues: require('../data/json/invalidParsedValues'),
firstColumnWrapCRLF: require('../data/json/firstColumnWrapCRLF.json'),
emptyLastFieldValue: require('../data/json/emptyLastFieldValue.json'),
emptyLastFieldValueNoEol: require('../data/json/emptyLastFieldValueNoEol.json')
emptyLastFieldValueNoEol: require('../data/json/emptyLastFieldValueNoEol.json'),
nativeMapMethod: require('../data/json/nativeMapMethod.json')
};
2 changes: 2 additions & 0 deletions test/data/csv/nativeMapMethod.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set,otherfieldname
test,test2
6 changes: 6 additions & 0 deletions test/data/json/nativeMapMethod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"set": "test",
"otherfieldname": "test2"
}
]
8 changes: 8 additions & 0 deletions test/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ function runTests(jsonTestData, csvTestData) {
done();
});
});

it('should properly handle headers with the same name as native Map methods', (done) => {
converter.json2csv(jsonTestData.nativeMapMethod, (err, csv) => {
if (err) done(err);
csv.should.equal(csvTestData.nativeMapMethod);
done();
});
});
});

describe('Error Handling', () => {
Expand Down

0 comments on commit e72fa24

Please sign in to comment.