Skip to content

Commit

Permalink
Rename duplicate headers (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortydegrees authored Nov 15, 2022
1 parent bde83f5 commit c1cbe16
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
31 changes: 31 additions & 0 deletions papaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,37 @@ License: MIT
if (!input)
return returnable();

// Rename headers if there are duplicates
if (config.header)
{
var firstLine = input.split(newline)[0];
var headers = firstLine.split(delim);
var separator = '_';
var headerMap = [];
var headerCount = {};
var duplicateHeaders = false;

for (var j in headers) {
var header = headers[j];
if (isFunction(config.transformHeader))
header = config.transformHeader(header, j);
var headerName = header;

var count = headerCount[header] || 0;
if (count > 0) {
duplicateHeaders = true;
headerName = header + separator + count;
}
headerCount[header] = count + 1;

headerMap.push(headerName);
}
if (duplicateHeaders) {
var editedInput = input.split(newline);
editedInput[0] = headerMap.join(delim);
input = editedInput.join(newline);
}
}
if (fastMode || (fastMode !== false && input.indexOf(quoteChar) === -1))
{
var rows = input.split(newline);
Expand Down
20 changes: 19 additions & 1 deletion tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,25 @@ var CORE_PARSER_TESTS = [
data: [['a', 'b', 'c'], ['']],
errors: []
}
}
},
{
description: "Simple duplicate header names",
input: 'A,A,A,A\n1,2,3,4',
config: { header: true },
expected: {
data: [['A', 'A_1', 'A_2', 'A_3'], ['1', '2', '3', '4']],
errors: []
}
},
{
description: "Duplicate header names with headerTransform",
input: 'A,A,A,A\n1,2,3,4',
config: { header: true, transformHeader: function(header) { return header.toLowerCase(); } },
expected: {
data: [['a', 'a_1', 'a_2', 'a_3'], ['1', '2', '3', '4']],
errors: []
}
},
];

describe('Core Parser Tests', function() {
Expand Down

0 comments on commit c1cbe16

Please sign in to comment.