From 265e09c67a16bea4e21ae9565defc6e913a2a0ea Mon Sep 17 00:00:00 2001 From: Jonathan Grimes Date: Mon, 4 Feb 2019 02:50:56 -0600 Subject: [PATCH] Ensure data is correctly parsed with header: true (#621) Fixes #619 --- papaparse.js | 48 +++++++++++++++++++++++++++++++-------------- tests/test-cases.js | 33 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/papaparse.js b/papaparse.js index e0ddca9..5ed703e 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1125,18 +1125,25 @@ 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)) { - header = _config.transformHeader(header); - } + function addHeder(header) + { + if (isFunction(_config.transformHeader)) + header = _config.transformHeader(header); - _fields.push(header); - } - _results.data.splice(0, 1); + _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) { @@ -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 row[field] = value; } - _results.data[i] = row; if (_config.header) { @@ -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; } diff --git a/tests/test-cases.js b/tests/test-cases.js index 0b34f0d..b0610dd 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -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']],