Browse Source

Handling extra fields with header row appropriately (fixes #83)

pull/93/head
Matthew Holt 11 years ago
parent
commit
423e02c923
  1. 3
      papaparse.js
  2. 93
      tests/test-cases.js
  3. 3
      tests/test-runner.js

3
papaparse.js

@ -791,7 +791,8 @@ @@ -791,7 +791,8 @@
row["__parsed_extra"] = [];
row["__parsed_extra"].push(_results.data[i][j]);
}
row[_fields[j]] = _results.data[i][j];
else
row[_fields[j]] = _results.data[i][j];
}
}

93
tests/test-cases.js

@ -139,6 +139,61 @@ var PARSE_TESTS = [ @@ -139,6 +139,61 @@ var PARSE_TESTS = [
]
}
},
{
description: "Header row with one row of data",
input: 'A,B,C\r\na,b,c',
config: { header: true },
expected: {
data: [{"A": "a", "B": "b", "C": "c"}],
errors: []
}
},
{
description: "Header row only",
input: 'A,B,C',
config: { header: true },
expected: {
data: [],
errors: []
}
},
{
description: "Row with too few fields",
input: 'A,B,C\r\na,b',
config: { header: true },
expected: {
data: [{"A": "a", "B": "b"}],
errors: [{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields: expected 3 fields but parsed 2",
"row": 0
}]
}
},
{
description: "Row with too many fields",
input: 'A,B,C\r\na,b,c,d,e\r\nf,g,h',
config: { header: true },
expected: {
data: [{"A": "a", "B": "b", "C": "c", "__parsed_extra": ["d", "e"]}, {"A": "f", "B": "g", "C": "h"}],
errors: [{
"type": "FieldMismatch",
"code": "TooManyFields",
"message": "Too many fields: expected 3 fields but parsed 5",
"row": 0
}]
}
},
{
description: "Row with enough fields but blank field at end",
input: 'A,B,C\r\na,b,',
config: { header: true },
expected: {
data: [{"A": "a", "B": "b", "C": ""}],
errors: []
}
},
{
description: "Tab delimiter",
input: 'a\tb\tc\r\nd\te\tf',
@ -185,6 +240,33 @@ var PARSE_TESTS = [ @@ -185,6 +240,33 @@ var PARSE_TESTS = [
errors: []
}
},
{
description: "Dynamic typing converts numeric literals",
input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2',
config: { dynamicTyping: true },
expected: {
data: [[1, 2.2, 1000], [-4, -4.5, -0.00004], ["-", "5a", "5-2"]],
errors: []
}
},
{
description: "Dynamic typing converts boolean literals",
input: 'true,false,T,F,TRUE,False',
config: { dynamicTyping: true },
expected: {
data: [[true, false, "T", "F", "TRUE", "False"]],
errors: []
}
},
{
description: "Dynamic typing doesn't convert other types",
input: 'A,B,C\r\nundefined,null,[\r\nvar,float,if',
config: { dynamicTyping: true },
expected: {
data: [["A", "B", "C"], ["undefined", "null", "["], ["var", "float", "if"]],
errors: []
}
},
{
description: "Commented line at beginning (comments: true)",
input: '# Comment!\r\na,b,c',
@ -205,10 +287,10 @@ var PARSE_TESTS = [ @@ -205,10 +287,10 @@ var PARSE_TESTS = [
},
{
description: "Commented line at end (comments: true)",
input: 'a,b,c\r\n# Comment',
input: 'a,true,false\r\n# Comment',
config: { comments: true },
expected: {
data: [['a', 'b', 'c']],
data: [['a', 'true', 'false']],
errors: []
}
},
@ -468,6 +550,13 @@ var PARSE_TESTS = [ @@ -468,6 +550,13 @@ var PARSE_TESTS = [
}
];
// Tests for Papa.parse() that involve asynchronous operation
var PARSE_ASYNC_TESTS = [
{
description: "Simple worker",

3
tests/test-runner.js

@ -182,6 +182,9 @@ function runParseTests(asyncDone) @@ -182,6 +182,9 @@ function runParseTests(asyncDone)
}
}
if (passed) // final check will catch any other differences
passed = JSON.stringify(actual) == JSON.stringify(expected);
// We pass back an object right now, even though it only contains
// one value, because we might add details to the test results later
// (same with compareErrors below)

Loading…
Cancel
Save