Browse Source

Add config option to transform values before dinamic typing (#505)

pull/506/merge
Eric Fossas 7 years ago committed by Sergi Almacellas Abellana
parent
commit
cbe39f630d
  1. 11
      papaparse.js
  2. 13
      tests/test-cases.js

11
papaparse.js

@ -205,6 +205,8 @@ @@ -205,6 +205,8 @@
}
_config.dynamicTyping = dynamicTyping;
_config.transform = isFunction(_config.transform) ? _config.transform : false;
if (_config.worker && Papa.WORKERS_SUPPORTED)
{
var w = newWorker();
@ -1087,7 +1089,7 @@ @@ -1087,7 +1089,7 @@
if (needsHeaderRow())
fillHeaderFields();
return applyHeaderAndDynamicTyping();
return applyHeaderAndDynamicTypingAndTransformation();
}
function needsHeaderRow()
@ -1139,9 +1141,9 @@ @@ -1139,9 +1141,9 @@
return value;
}
function applyHeaderAndDynamicTyping()
function applyHeaderAndDynamicTypingAndTransformation()
{
if (!_results || (!_config.header && !_config.dynamicTyping))
if (!_results || (!_config.header && !_config.dynamicTyping && !_config.transform))
return _results;
for (var i = 0; i < _results.data.length; i++)
@ -1157,6 +1159,9 @@ @@ -1157,6 +1159,9 @@
if (_config.header)
field = j >= _fields.length ? '__parsed_extra' : _fields[j];
if (_config.transform)
value = _config.transform(value,field);
value = parseDynamic(field, value);
if (field === '__parsed_extra')

13
tests/test-cases.js

@ -872,6 +872,19 @@ var PARSE_TESTS = [ @@ -872,6 +872,19 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Custom transform function is applied to values",
input: 'A,B,C\r\nd,e,f',
config: {
transform: function(value) {
return value.toLowerCase();
}
},
expected: {
data: [["a","b","c"], ["d","e","f"]],
errors: []
}
},
{
description: "Blank line at beginning",
input: '\r\na,b,c\r\nd,e,f',

Loading…
Cancel
Save