Skip to content

Commit

Permalink
Implement mholt#612
Browse files Browse the repository at this point in the history
  • Loading branch information
theLAZYmd committed Nov 3, 2021
1 parent 23e1b47 commit c0acd22
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
15 changes: 13 additions & 2 deletions docs/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,19 @@ <h5 id="config-details">Config Options</h5>
<code>transformHeader</code>
</td>
<td>
A function to apply on each header. Requires <code>header</code> to be <code>true</code>. The function receives the header as its first argument and the index as second.<br>
Only available starting with version 5.0.
A function to apply on each header. Requires <code>header</code> to be <code>true</code>. The function receives the header as its first argument and the index (the column number) as second. When used in conjunction with config.headerLines, the function receives a third and fourth parameter which are:
<ul>
<li>the existing header for that column based on previous lines iteration ('' if no previous lines)</li>
<li>the rowNumber (0 for the first line)</li>
</ul>
</td>
</tr>
<tr>
<td>
<code>headerLines</code>
</td>
<td>
The number of rows which will be used to transform into a header, and removed from the rest of the data. Requires header to be true. Default 1.
</td>
</tr>
<tr>
Expand Down
20 changes: 11 additions & 9 deletions papaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,24 +1180,26 @@ License: MIT
if (!_results)
return;

function addHeader(header, i)
var headerLines = _config.headerLines || 1;
function addHeader(j, header, i)
{
if (isFunction(_config.transformHeader))
header = _config.transformHeader(header, i);
header = _config.transformHeader(header, i, _fields[i] || '', j);

_fields.push(header);
_fields[i] = header;
}

if (Array.isArray(_results.data[0]))
{
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++)
_results.data[i].forEach(addHeader);

_results.data.splice(0, 1);
for (var j = 0; j < Math.min(headerLines, _results.data.length); j++)
{
// A function which takes two arguments (header, i) where j is set by the fact it is called in this loop. It then calls addHeader() with all three arguments
_results.data[j].forEach(addHeader.bind(null, j));
}
_results.data.splice(0, headerLines);
}
// if _results.data[0] is not an array, we are in a step where _results.data is the row.
else
_results.data.forEach(addHeader);
_results.data.forEach(addHeader.bind(null, 0));
}

function shouldApplyDynamicTyping(field) {
Expand Down
Loading

0 comments on commit c0acd22

Please sign in to comment.