theLAZYmd 4 years ago
parent
commit
c0acd22ed2
  1. 15
      docs/docs.html
  2. 20
      papaparse.js
  3. 2
      papaparse.min.js
  4. 9
      tests/test-cases.js

15
docs/docs.html

@ -510,8 +510,19 @@ var csv = Papa.unparse({
<code>transformHeader</code> <code>transformHeader</code>
</td> </td>
<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> 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:
Only available starting with version 5.0. <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> </td>
</tr> </tr>
<tr> <tr>

20
papaparse.js

@ -1180,24 +1180,26 @@ License: MIT
if (!_results) if (!_results)
return; return;
function addHeader(header, i) var headerLines = _config.headerLines || 1;
function addHeader(j, header, i)
{ {
if (isFunction(_config.transformHeader)) 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])) if (Array.isArray(_results.data[0]))
{ {
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) for (var j = 0; j < Math.min(headerLines, _results.data.length); j++)
_results.data[i].forEach(addHeader); {
// 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.splice(0, 1); _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. // if _results.data[0] is not an array, we are in a step where _results.data is the row.
else else
_results.data.forEach(addHeader); _results.data.forEach(addHeader.bind(null, 0));
} }
function shouldApplyDynamicTyping(field) { function shouldApplyDynamicTyping(field) {

2
papaparse.min.js vendored

File diff suppressed because one or more lines are too long

9
tests/test-cases.js

@ -767,6 +767,15 @@ var PARSE_TESTS = [
errors: [] errors: []
} }
}, },
{
description: "Header row is generated from multiple rows when transformheader function is provided and config.headerLines is set",
input: "A,B,C\r\nD,E,F\r\nG,H,I\r\nadg,beh,cfi",
config: { header: true, transformHeader: function(header, i, acc, j) { return acc + (j % 2 ? header.toLowerCase() : header); }, headerLines: 3 },
expected: {
data: [{"AdG": "adg", "BeH": "beh", "CfI": "cfi"}],
errors: []
}
},
{ {
description: "transformHeader accepts and optional index attribute", description: "transformHeader accepts and optional index attribute",
input: 'A,B,C\r\na,b,c', input: 'A,B,C\r\na,b,c',

Loading…
Cancel
Save