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

Validate field titles against Object.keys list #186

Merged
merged 3 commits into from
May 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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