Browse Source

Allow transforming header columns (#589)

* Deprecate trimHeaders: The same result can be achieved by using transformHeader parameter.
pull/596/head
João Sardinha 6 years ago committed by Sergi Almacellas Abellana
parent
commit
c95db1f64c
  1. 6
      docs/docs.html
  2. 4
      papaparse.js
  3. 8
      tests/test-cases.js

6
docs/docs.html

@ -340,7 +340,7 @@ var csv = Papa.unparse({ @@ -340,7 +340,7 @@ var csv = Papa.unparse({
quoteChar: '"',
escapeChar: '"',
header: false,
trimHeaders: false,
transformHeader: undefined,
dynamicTyping: false,
preview: 0,
encoding: "",
@ -412,10 +412,10 @@ var csv = Papa.unparse({ @@ -412,10 +412,10 @@ var csv = Papa.unparse({
</tr>
<tr>
<td>
<code>trimHeaders</code>
<code>transformHeader</code>
</td>
<td>
If true leading/trailing spaces will be trimed from headers.
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.
</td>
</tr>
<tr>

4
papaparse.js

@ -1151,8 +1151,8 @@ if (!Array.isArray) @@ -1151,8 +1151,8 @@ if (!Array.isArray)
{
var header = _results.data[i][j];
if (_config.trimHeaders) {
header = header.trim();
if (isFunction(_config.transformHeader)) {
header = _config.transformHeader(header);
}
_fields.push(header);

8
tests/test-cases.js

@ -725,11 +725,11 @@ var PARSE_TESTS = [ @@ -725,11 +725,11 @@ var PARSE_TESTS = [
}
},
{
description: "Header rows are trimmed when trimHeaders is set",
input: ' A , B , C \r\na,b ,c',
config: { header: true, trimHeaders: true },
description: "Header rows are transformed when transformHeader function is provided",
input: 'A,B,C\r\na,b,c',
config: { header: true, transformHeader: function(header) { return header.toLowerCase(); } },
expected: {
data: [{"A": "a", "B": "b ", "C": "c"}],
data: [{"a": "a", "b": "b", "c": "c"}],
errors: []
}
},

Loading…
Cancel
Save