From cbe39f630d841ca120e1c95ebd0e3e27e7765a0b Mon Sep 17 00:00:00 2001 From: Eric Fossas Date: Mon, 4 Jun 2018 03:46:38 -0600 Subject: [PATCH] Add config option to transform values before dinamic typing (#505) --- papaparse.js | 11 ++++++++--- tests/test-cases.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/papaparse.js b/papaparse.js index bc73666..b289f11 100755 --- a/papaparse.js +++ b/papaparse.js @@ -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 @@ if (needsHeaderRow()) fillHeaderFields(); - return applyHeaderAndDynamicTyping(); + return applyHeaderAndDynamicTypingAndTransformation(); } function needsHeaderRow() @@ -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 @@ 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') diff --git a/tests/test-cases.js b/tests/test-cases.js index b10c386..7d72762 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -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',