From 621ad543bca27699e01237712be6133d2327fdba Mon Sep 17 00:00:00 2001 From: vyrak bunleang Date: Thu, 6 Apr 2017 10:22:51 -0600 Subject: [PATCH] able to configure as function --- papaparse.js | 8 ++++++++ tests/test-cases.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/papaparse.js b/papaparse.js index 23139cf..068d331 100644 --- a/papaparse.js +++ b/papaparse.js @@ -426,6 +426,10 @@ this._isAllDynamic = true; return; } + else if (typeof config === 'function') + { + this._isIncluded = config; + } else if (!config) { return; @@ -456,6 +460,10 @@ DynamicTypingConfig.prototype.constructor = DynamicTypingConfig; DynamicTypingConfig.prototype.isIncluded = function(field) { + if (typeof this._isIncluded === 'function') + { + return this._isIncluded(field); + } return this._isAllDynamic || this._columnConfig[field] === true || (this._isBlackListOnly && this._columnConfig[field] !== false) }; diff --git a/tests/test-cases.js b/tests/test-cases.js index 7bd05f6..9cbabd8 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -720,6 +720,15 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Dynamic typing by indices can be determined by function", + input: '001,002,003', + config: { dynamicTyping: function(field) { return (field % 2) === 0; } }, + expected: { + data: [[1, "002", 3]], + errors: [] + } + }, { description: "Dynamic typing can be applied to `__only` headers", input: 'A,B,C\r\n001,002,003', @@ -747,6 +756,15 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Dynamic typing by headers can be determined by function", + input: 'A_as_int,B,C_as_int\r\n001,002,003', + config: { header: true, dynamicTyping: function(field) { return /_as_int$/.test(field); } }, + expected: { + data: [{"A_as_int": 1, "B": "002", "C_as_int": 3}], + errors: [] + } + }, { description: "Blank line at beginning", input: '\r\na,b,c\r\nd,e,f',