Browse Source

Add row counter accurate row reporting on errors with chunked parsing (#509)

pull/515/head
Mauricio Villalobos 7 years ago committed by Sergi Almacellas Abellana
parent
commit
f1f674a8cb
  1. 7
      papaparse.js
  2. 37
      tests/node-tests.js
  3. 2001
      tests/verylong-sample.csv

7
papaparse.js

@ -957,6 +957,7 @@
var self = this; var self = this;
var _stepCounter = 0; // Number of times step was called (number of rows parsed) var _stepCounter = 0; // Number of times step was called (number of rows parsed)
var _rowCounter = 0; // Number of rows that have been parsed so far
var _input; // The input being parsed var _input; // The input being parsed
var _parser; // The core parser being used var _parser; // The core parser being used
var _paused = false; // Whether we are paused or not var _paused = false; // Whether we are paused or not
@ -1172,14 +1173,16 @@
if (_config.header) if (_config.header)
{ {
if (j > _fields.length) if (j > _fields.length)
addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, i); addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
else if (j < _fields.length) else if (j < _fields.length)
addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, i); addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, _rowCounter + i);
} }
} }
if (_config.header && _results.meta) if (_config.header && _results.meta)
_results.meta.fields = _fields; _results.meta.fields = _fields;
_rowCounter += _results.data.length;
return _results; return _results;
} }

37
tests/node-tests.js

@ -59,6 +59,43 @@ describe('PapaParse', function() {
}); });
}); });
it('reports the correct row number on FieldMismatch errors', function(done) {
Papa.parse(fs.createReadStream(__dirname + '/verylong-sample.csv'), {
header: true,
fastMode: true,
complete: function(parsedCsv) {
assert.deepEqual(parsedCsv.errors, [
{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields: expected 3 fields but parsed 2",
"row": 498
},
{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields: expected 3 fields but parsed 2",
"row": 998
},
{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields: expected 3 fields but parsed 2",
"row": 1498
},
{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields: expected 3 fields but parsed 2",
"row": 1998
}
]);
assert.strictEqual(2000, parsedCsv.data.length);
done();
},
});
});
it('piped streaming CSV should be correctly parsed', function(done) { it('piped streaming CSV should be correctly parsed', function(done) {
var data = []; var data = [];
var readStream = fs.createReadStream(__dirname + '/long-sample.csv', 'utf8'); var readStream = fs.createReadStream(__dirname + '/long-sample.csv', 'utf8');

2001
tests/verylong-sample.csv

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save