Skip to content

Commit

Permalink
Convert CSV column names with spaces to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
timroes committed Aug 13, 2016
1 parent 3e2fee2 commit 2e24b4e
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,24 @@ modules.get('apps/management')
return;
}
if (row === 1) {
// Convert column names with spaces in them to camel case
const columnNames = results.meta.fields.map((column) => {
return /\s/g.test(column) ? _.camelCase(column) : column;
});

// Collect general information on the first pass
if (results.meta.fields.length > _.uniq(results.meta.fields).length) {
this.formattedErrors.push('Column names must be unique');
if (columnNames.length > _.uniq(columnNames).length) {
let errorMsg = 'Column names must be unique';
if (results.meta.fields.length === _.unique(results.meta.fields).length) {
// If the original names (not the camel cased ones) would have been unique
// add a hin about the camel casing to the error.
errorMsg += ' (be aware that column names with spaces were converted to camel case)';
}
this.formattedErrors.push(errorMsg);
}

let hasEmptyHeader = false;
_.forEach(results.meta.fields, (field) => {
_.forEach(columnNames, (field) => {
if (_.isEmpty(field)) {
hasEmptyHeader = true;
}
Expand All @@ -82,11 +93,11 @@ modules.get('apps/management')
this.formattedErrors.push('Column names must not be blank');
}

if (results.meta.fields.length > maxSampleColumns) {
if (columnNames.length > maxSampleColumns) {
this.formattedWarnings.push(`Preview truncated to ${maxSampleColumns} columns`);
}

this.columns = results.meta.fields.slice(0, maxSampleColumns);
this.columns = columnNames.slice(0, maxSampleColumns);
this.parseOptions = _.defaults({}, this.parseOptions, {delimiter: results.meta.delimiter});
}

Expand Down

0 comments on commit 2e24b4e

Please sign in to comment.