Browse Source

Ensure data is correctly parsed with header: true (#621)

Fixes #619
pull/628/head
Jonathan Grimes 6 years ago committed by Sergi Almacellas Abellana
parent
commit
265e09c67a
  1. 40
      papaparse.js
  2. 33
      tests/test-cases.js

40
papaparse.js

@ -1125,19 +1125,26 @@ License: MIT @@ -1125,19 +1125,26 @@ License: MIT
{
if (!_results)
return;
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++)
for (var j = 0; j < _results.data[i].length; j++)
{
var header = _results.data[i][j];
if (isFunction(_config.transformHeader)) {
function addHeder(header)
{
if (isFunction(_config.transformHeader))
header = _config.transformHeader(header);
}
_fields.push(header);
}
if (Array.isArray(_results.data[0]))
{
for (var i = 0; needsHeaderRow() && i < _results.data.length; i++)
_results.data[i].forEach(addHeder);
_results.data.splice(0, 1);
}
// if _results.data[0] is not an array, we are in a step where _results.data is the row.
else
_results.data.forEach(addHeder);
}
function shouldApplyDynamicTyping(field) {
// Cache function values to avoid calling it for each row
@ -1170,15 +1177,15 @@ License: MIT @@ -1170,15 +1177,15 @@ License: MIT
if (!_results || (!_config.header && !_config.dynamicTyping && !_config.transform))
return _results;
for (var i = 0; i < _results.data.length; i++)
function processRow(rowSource, i)
{
var row = _config.header ? {} : [];
var j;
for (j = 0; j < _results.data[i].length; j++)
for (j = 0; j < rowSource.length; j++)
{
var field = j;
var value = _results.data[i][j];
var value = rowSource[j];
if (_config.header)
field = j >= _fields.length ? '__parsed_extra' : _fields[j];
@ -1197,7 +1204,6 @@ License: MIT @@ -1197,7 +1204,6 @@ License: MIT
row[field] = value;
}
_results.data[i] = row;
if (_config.header)
{
@ -1206,12 +1212,24 @@ License: MIT @@ -1206,12 +1212,24 @@ License: MIT
else if (j < _fields.length)
addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
}
return row;
}
var incrementBy = 1;
if (!_results.data[0] || Array.isArray(_results.data[0]))
{
_results.data = _results.data.map(processRow);
incrementBy = _results.data.length;
}
else
_results.data = processRow(_results.data, 0);
if (_config.header && _results.meta)
_results.meta.fields = _fields;
_rowCounter += _results.data.length;
_rowCounter += incrementBy;
return _results;
}

33
tests/test-cases.js

@ -1804,6 +1804,39 @@ var CUSTOM_TESTS = [ @@ -1804,6 +1804,39 @@ var CUSTOM_TESTS = [
});
}
},
{
description: "Data is correctly parsed with steps (headers)",
expected: [{One: 'A', Two: 'b', Three: 'c'}, {One: 'd', Two: 'E', Three: 'f'}],
run: function(callback) {
var data = [];
Papa.parse('One,Two,Three\nA,b,c\nd,E,f', {
header: true,
step: function(results) {
data.push(results.data);
},
complete: function() {
callback(data);
}
});
}
},
{
description: "Data is correctly parsed with steps and worker (headers)",
expected: [{One: 'A', Two: 'b', Three: 'c'}, {One: 'd', Two: 'E', Three: 'f'}],
run: function(callback) {
var data = [];
Papa.parse('One,Two,Three\nA,b,c\nd,E,f', {
header: true,
worker: true,
step: function(results) {
data.push(results.data);
},
complete: function() {
callback(data);
}
});
}
},
{
description: "Data is correctly parsed with steps and worker",
expected: [['A', 'b', 'c'], ['d', 'E', 'f']],

Loading…
Cancel
Save