From 423e02c923bf8e6838495d580bcd7a6629dd2b73 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 1 Sep 2014 18:03:02 -0600 Subject: [PATCH] Handling extra fields with header row appropriately (fixes #83) --- papaparse.js | 3 +- tests/test-cases.js | 93 +++++++++++++++++++++++++++++++++++++++++++- tests/test-runner.js | 3 ++ 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/papaparse.js b/papaparse.js index f207759..6439f81 100644 --- a/papaparse.js +++ b/papaparse.js @@ -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]; } } diff --git a/tests/test-cases.js b/tests/test-cases.js index da06847..db9bb46 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -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 = [ 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 = [ }, { 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 = [ } ]; + + + + + + +// Tests for Papa.parse() that involve asynchronous operation var PARSE_ASYNC_TESTS = [ { description: "Simple worker", diff --git a/tests/test-runner.js b/tests/test-runner.js index d8b9f73..2cd9a47 100644 --- a/tests/test-runner.js +++ b/tests/test-runner.js @@ -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)