Browse Source

Merge 1a0e4825ec into b58a65c607

pull/593/merge
Erik-HH 7 years ago committed by GitHub
parent
commit
270962b69a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      papaparse.js
  2. 52
      tests/test-cases.js

4
papaparse.js

@ -1266,7 +1266,7 @@ if (!Array.isArray)
if (typeof fieldCountPrevRow === 'undefined') if (typeof fieldCountPrevRow === 'undefined')
{ {
fieldCountPrevRow = fieldCount; fieldCountPrevRow = 0;
continue; continue;
} }
else if (fieldCount > 1) else if (fieldCount > 1)
@ -1279,7 +1279,7 @@ if (!Array.isArray)
if (preview.data.length > 0) if (preview.data.length > 0)
avgFieldCount /= (preview.data.length - emptyLinesCount); avgFieldCount /= (preview.data.length - emptyLinesCount);
if ((typeof bestDelta === 'undefined' || delta < bestDelta) if ((typeof bestDelta === 'undefined' || delta > bestDelta)
&& avgFieldCount > 1.99) && avgFieldCount > 1.99)
{ {
bestDelta = delta; bestDelta = delta;

52
tests/test-cases.js

@ -13,13 +13,15 @@ var FILES_ENABLED = false;
try { try {
new File([""], ""); // eslint-disable-line no-new new File([""], ""); // eslint-disable-line no-new
FILES_ENABLED = true; FILES_ENABLED = true;
} catch (e) {} // safari, ie } catch (e) {
} // safari, ie
var XHR_ENABLED = false; var XHR_ENABLED = false;
try { try {
new XMLHttpRequest(); // eslint-disable-line no-new new XMLHttpRequest(); // eslint-disable-line no-new
XHR_ENABLED = true; XHR_ENABLED = true;
} catch (e) {} // safari, ie } catch (e) {
} // safari, ie
// Tests for the core parser using new Papa.Parser().parse() (CSV to JSON) // Tests for the core parser using new Papa.Parser().parse() (CSV to JSON)
var CORE_PARSER_TESTS = [ var CORE_PARSER_TESTS = [
@ -415,6 +417,14 @@ var CORE_PARSER_TESTS = [
errors: [] errors: []
} }
}, },
{
description: "Pipes with decimal numbers and comma as decimal separator",
input: 'a|3,4|b\r\nc|3,4|d',
expected: {
data: [['a', '3,4', 'b'], ['c', '3,4', 'd']],
errors: []
}
},
{ {
description: "Commented line at end", description: "Commented line at end",
input: 'a,true,false\n# Comment', input: 'a,true,false\n# Comment',
@ -594,7 +604,6 @@ describe('Core Parser Tests', function() {
}); });
// Tests for Papa.parse() function -- high-level wrapped parser (CSV to JSON) // Tests for Papa.parse() function -- high-level wrapped parser (CSV to JSON)
var PARSE_TESTS = [ var PARSE_TESTS = [
{ {
@ -807,7 +816,11 @@ var PARSE_TESTS = [
{ {
description: "Callback delimiter", description: "Callback delimiter",
input: 'a$ b$ c', input: 'a$ b$ c',
config: { delimiter: function(input) { return input[1] + ' '; } }, config: {
delimiter: function(input) {
return input[1] + ' ';
}
},
expected: { expected: {
data: [['a', 'b', 'c']], data: [['a', 'b', 'c']],
errors: [] errors: []
@ -875,7 +888,11 @@ var PARSE_TESTS = [
{ {
description: "Dynamic typing by indices can be determined by function", description: "Dynamic typing by indices can be determined by function",
input: '001,002,003', input: '001,002,003',
config: { dynamicTyping: function(field) { return (field % 2) === 0; } }, config: {
dynamicTyping: function(field) {
return (field % 2) === 0;
}
},
expected: { expected: {
data: [[1, "002", 3]], data: [[1, "002", 3]],
errors: [] errors: []
@ -884,7 +901,11 @@ var PARSE_TESTS = [
{ {
description: "Dynamic typing by headers can be determined by function", description: "Dynamic typing by headers can be determined by function",
input: 'A_as_int,B,C_as_int\r\n001,002,003', input: 'A_as_int,B,C_as_int\r\n001,002,003',
config: { header: true, dynamicTyping: function(field) { return /_as_int$/.test(field); } }, config: {
header: true, dynamicTyping: function(field) {
return /_as_int$/.test(field);
}
},
expected: { expected: {
data: [{"A_as_int": 1, "B": "002", "C_as_int": 3}], data: [{"A_as_int": 1, "B": "002", "C_as_int": 3}],
errors: [] errors: []
@ -1380,7 +1401,6 @@ describe('Parse Tests', function() {
}); });
// Tests for Papa.parse() that involve asynchronous operation // Tests for Papa.parse() that involve asynchronous operation
var PARSE_ASYNC_TESTS = [ var PARSE_ASYNC_TESTS = [
{ {
@ -1423,8 +1443,7 @@ var PARSE_ASYNC_TESTS = [
description: "Simple file", description: "Simple file",
disabled: !FILES_ENABLED, disabled: !FILES_ENABLED,
input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z"], "sample.csv") : false, input: FILES_ENABLED ? new File(["A,B,C\nX,Y,Z"], "sample.csv") : false,
config: { config: {},
},
expected: { expected: {
data: [['A', 'B', 'C'], ['X', 'Y', 'Z']], data: [['A', 'B', 'C'], ['X', 'Y', 'Z']],
errors: [] errors: []
@ -1469,7 +1488,6 @@ describe('Parse Async Tests', function() {
}); });
// Tests for Papa.unparse() function (JSON to CSV) // Tests for Papa.unparse() function (JSON to CSV)
var UNPARSE_TESTS = [ var UNPARSE_TESTS = [
{ {
@ -1634,7 +1652,10 @@ var UNPARSE_TESTS = [
}, },
{ {
description: "Date objects are exported in its ISO representation", description: "Date objects are exported in its ISO representation",
input: [{date: new Date("2018-05-04T21:08:03.269Z"), "not a date": 16}, {date: new Date("Tue May 08 2018 08:20:22 GMT-0700 (PDT)"), "not a date": 32}], input: [{
date: new Date("2018-05-04T21:08:03.269Z"),
"not a date": 16
}, {date: new Date("Tue May 08 2018 08:20:22 GMT-0700 (PDT)"), "not a date": 32}],
expected: 'date,not a date\r\n2018-05-04T21:08:03.269Z,16\r\n2018-05-08T15:20:22.000Z,32' expected: 'date,not a date\r\n2018-05-04T21:08:03.269Z,16\r\n2018-05-08T15:20:22.000Z,32'
}, },
{ {
@ -1699,7 +1720,6 @@ describe('Unparse Tests', function() {
}); });
var CUSTOM_TESTS = [ var CUSTOM_TESTS = [
{ {
description: "Complete is called with all results if neither step nor chunk is defined", description: "Complete is called with all results if neither step nor chunk is defined",
@ -2109,12 +2129,16 @@ var CUSTOM_TESTS = [
'?x=1&papaworker&y=1', '?x=1&papaworker&y=1',
'?x=1&papaworker=1' '?x=1&papaworker=1'
]; ];
var results = searchStrings.map(function() { return false; }); var results = searchStrings.map(function() {
return false;
});
var workers = []; var workers = [];
// Give it .5s to do something // Give it .5s to do something
setTimeout(function() { setTimeout(function() {
workers.forEach(function(w) { w.terminate(); }); workers.forEach(function(w) {
w.terminate();
});
callback(results); callback(results);
}, 500); }, 500);

Loading…
Cancel
Save