Skip to content

Commit

Permalink
Support dynamic typing on explicitly defined columns
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall007 committed Jul 9, 2016
1 parent caf0002 commit 7225d8e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
49 changes: 29 additions & 20 deletions papaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
function CsvToJson(_input, _config)
{
_config = _config || {};
_config.dynamicTyping = _config.dynamicTyping || false;

if (_config.worker && Papa.WORKERS_SUPPORTED)
{
Expand Down Expand Up @@ -845,44 +846,52 @@
_results.data.splice(0, 1);
}

function parseDynamic(field, value)
{
if ((_config.dynamicTyping[field] || _config.dynamicTyping) === true)
{
if (value === 'true' || value === 'TRUE')
return true;
else if (value === 'false' || value === 'FALSE')
return false;
else
return tryParseFloat(value);
}
return value;
}

function applyHeaderAndDynamicTyping()
{
if (!_results || (!_config.header && !_config.dynamicTyping))
return _results;

for (var i = 0; i < _results.data.length; i++)
{
var row = {};
var row = _config.header ? {} : [];

for (var j = 0; j < _results.data[i].length; j++)
{
if (_config.dynamicTyping)
{
var value = _results.data[i][j];
if (value === 'true' || value === 'TRUE')
_results.data[i][j] = true;
else if (value === 'false' || value === 'FALSE')
_results.data[i][j] = false;
else
_results.data[i][j] = tryParseFloat(value);
}
var field = j;
var value = _results.data[i][j];

if (_config.header)
field = j >= _fields.length ? '__parsed_extra' : _fields[j];

value = parseDynamic(field, value);

if (field === '__parsed_extra')
{
if (j >= _fields.length)
{
if (!row['__parsed_extra'])
row['__parsed_extra'] = [];
row['__parsed_extra'].push(_results.data[i][j]);
}
else
row[_fields[j]] = _results.data[i][j];
row[field] = row[field] || [];
row[field].push(value);
}
else
row[field] = value;
}

_results.data[i] = row;

if (_config.header)
{
_results.data[i] = row;
if (j > _fields.length)
addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, i);
else if (j < _fields.length)
Expand Down
32 changes: 32 additions & 0 deletions tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,38 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Dynamic typing applies to specific columns",
input: 'A,B,C\r\n1,2.2,1e3\r\n-4,-4.5,-4e-5',
config: { header: true, dynamicTyping: { A: true, C: true } },
expected: {
data: [{"A": 1, "B": "2.2", "C": 1000}, {"A": -4, "B": "-4.5", "C": -0.00004}],
errors: []
}
},
{
description: "Dynamic typing applies to specific columns by index",
input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2',
config: { dynamicTyping: { 1: true } },
expected: {
data: [["1", 2.2, "1e3"], ["-4", -4.5, "-4e-5"], ["-", "5a", "5-2"]],
errors: []
}
},
{
description: "Dynamic typing can be applied to `__parsed_extra`",
input: 'A,B,C\r\n1,2.2,1e3,5.5\r\n-4,-4.5,-4e-5',
config: { header: true, dynamicTyping: { A: true, C: true, __parsed_extra: true } },
expected: {
data: [{"A": 1, "B": "2.2", "C": 1000, "__parsed_extra": [ 5.5 ]}, {"A": -4, "B": "-4.5", "C": -0.00004}],
errors: [{
"type": "FieldMismatch",
"code": "TooManyFields",
"message": "Too many fields: expected 3 fields but parsed 4",
"row": 0
}]
}
},
{
description: "Blank line at beginning",
input: '\r\na,b,c\r\nd,e,f',
Expand Down

0 comments on commit 7225d8e

Please sign in to comment.