Browse Source

able to configure as function

pull/377/head
vyrak bunleang 8 years ago
parent
commit
621ad543bc
  1. 8
      papaparse.js
  2. 18
      tests/test-cases.js

8
papaparse.js

@ -426,6 +426,10 @@
this._isAllDynamic = true; this._isAllDynamic = true;
return; return;
} }
else if (typeof config === 'function')
{
this._isIncluded = config;
}
else if (!config) else if (!config)
{ {
return; return;
@ -456,6 +460,10 @@
DynamicTypingConfig.prototype.constructor = DynamicTypingConfig; DynamicTypingConfig.prototype.constructor = DynamicTypingConfig;
DynamicTypingConfig.prototype.isIncluded = function(field) 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) return this._isAllDynamic || this._columnConfig[field] === true || (this._isBlackListOnly && this._columnConfig[field] !== false)
}; };

18
tests/test-cases.js

@ -720,6 +720,15 @@ var PARSE_TESTS = [
errors: [] 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", description: "Dynamic typing can be applied to `__only` headers",
input: 'A,B,C\r\n001,002,003', input: 'A,B,C\r\n001,002,003',
@ -747,6 +756,15 @@ var PARSE_TESTS = [
errors: [] 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", description: "Blank line at beginning",
input: '\r\na,b,c\r\nd,e,f', input: '\r\na,b,c\r\nd,e,f',

Loading…
Cancel
Save