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({
quoteChar: '"', quoteChar: '"',
escapeChar: '"', escapeChar: '"',
header: false, header: false,
trimHeaders: false, transformHeader: undefined,
dynamicTyping: false, dynamicTyping: false,
preview: 0, preview: 0,
encoding: "", encoding: "",
@ -412,10 +412,10 @@ var csv = Papa.unparse({
</tr> </tr>
<tr> <tr>
<td> <td>
<code>trimHeaders</code> <code>transformHeader</code>
</td> </td>
<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> </td>
</tr> </tr>
<tr> <tr>

4
papaparse.js

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

8
tests/test-cases.js

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

Loading…
Cancel
Save