Browse Source

Improve transform function docs and tests. Fixes #601

pull/602/head
Sergi Almacellas Abellana 6 years ago
parent
commit
ae982483cb
  1. 2
      docs/docs.html
  2. 33
      tests/test-cases.js

2
docs/docs.html

@ -545,7 +545,7 @@ var csv = Papa.unparse({
<code>transform</code> <code>transform</code>
</td> </td>
<td> <td>
A function to apply on each value. The function receives the value as its first argument and the column number as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping. A function to apply on each value. The function receives the value as its first argument and the column number or header name when enabled as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping.
</td> </td>
</tr> </tr>
</table> </table>

33
tests/test-cases.js

@ -913,6 +913,39 @@ var PARSE_TESTS = [
errors: [] errors: []
} }
}, },
{
description: "Custom transform accepts column number also",
input: 'A,B,C\r\nd,e,f',
config: {
transform: function(value, column) {
if (column % 2) {
value = value.toLowerCase();
}
return value;
}
},
expected: {
data: [["A","b","C"], ["d","e","f"]],
errors: []
}
},
{
description: "Custom transform accepts header name when using header",
input: 'A,B,C\r\nd,e,f',
config: {
header: true,
transform: function(value, name) {
if (name === 'B') {
value = value.toUpperCase();
}
return value;
}
},
expected: {
data: [{'A': "d", 'B': "E", 'C': "f"}],
errors: []
}
},
{ {
description: "Dynamic typing converts ISO date strings to Dates", description: "Dynamic typing converts ISO date strings to Dates",
input: 'ISO date,long date\r\n2018-05-04T21:08:03.269Z,Fri May 04 2018 14:08:03 GMT-0700 (PDT)\r\n2018-05-08T15:20:22.642Z,Tue May 08 2018 08:20:22 GMT-0700 (PDT)', input: 'ISO date,long date\r\n2018-05-04T21:08:03.269Z,Fri May 04 2018 14:08:03 GMT-0700 (PDT)\r\n2018-05-08T15:20:22.642Z,Tue May 08 2018 08:20:22 GMT-0700 (PDT)',

Loading…
Cancel
Save