Browse Source

Merge branch 'master' into iso_dates

pull/506/head
Sergi Almacellas Abellana 7 years ago committed by GitHub
parent
commit
9c0c6476f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      papaparse.js
  2. 37
      tests/node-tests.js
  3. 11
      tests/test-cases.js
  4. 2001
      tests/verylong-sample.csv

18
papaparse.js

@ -205,6 +205,8 @@ @@ -205,6 +205,8 @@
}
_config.dynamicTyping = dynamicTyping;
_config.transform = isFunction(_config.transform) ? _config.transform : false;
if (_config.worker && Papa.WORKERS_SUPPORTED)
{
var w = newWorker();
@ -961,6 +963,7 @@ @@ -961,6 +963,7 @@
var self = this;
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 _parser; // The core parser being used
var _paused = false; // Whether we are paused or not
@ -1090,7 +1093,7 @@ @@ -1090,7 +1093,7 @@
if (needsHeaderRow())
fillHeaderFields();
return applyHeaderAndDynamicTyping();
return applyHeaderAndDynamicTypingAndTransformation();
}
function needsHeaderRow()
@ -1142,9 +1145,9 @@ @@ -1142,9 +1145,9 @@
return value;
}
function applyHeaderAndDynamicTyping()
function applyHeaderAndDynamicTypingAndTransformation()
{
if (!_results || (!_config.header && !_config.dynamicTyping))
if (!_results || (!_config.header && !_config.dynamicTyping && !_config.transform))
return _results;
for (var i = 0; i < _results.data.length; i++)
@ -1160,6 +1163,9 @@ @@ -1160,6 +1163,9 @@
if (_config.header)
field = j >= _fields.length ? '__parsed_extra' : _fields[j];
if (_config.transform)
value = _config.transform(value,field);
value = parseDynamic(field, value);
if (field === '__parsed_extra')
@ -1176,14 +1182,16 @@ @@ -1176,14 +1182,16 @@
if (_config.header)
{
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)
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)
_results.meta.fields = _fields;
_rowCounter += _results.data.length;
return _results;
}

37
tests/node-tests.js

@ -59,6 +59,43 @@ describe('PapaParse', function() { @@ -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) {
var data = [];
var readStream = fs.createReadStream(__dirname + '/long-sample.csv', 'utf8');

11
tests/test-cases.js

@ -872,6 +872,17 @@ var PARSE_TESTS = [ @@ -872,6 +872,17 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Custom transform function is applied to values",
input: 'A,B,C\r\nd,e,f',
config: {
transform: function(value) {
return value.toLowerCase();
}
},
expected: {
data: [["a","b","c"], ["d","e","f"]],
},
{
description: "Dynamic typing converts ISO date strings to Dates",
input: 'ISO date,long date\r\n2018-05-04T21:08:03.269Z,Fri May 04 2018 14:08:03 GMT-0700 (PDT)\r\n2018-05-08T15:20:22.642Z,Tue May 08 2018 08:20:22 GMT-0700 (PDT)',

2001
tests/verylong-sample.csv

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