From 8af1d23646ecf15ccd7cdbf6d84966e3ebdc6fa0 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Wed, 9 Jan 2019 09:58:24 +0100 Subject: [PATCH] Correctly guess deliminter when mixed with commas Closes #592 --- papaparse.js | 4 ++-- tests/test-cases.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/papaparse.js b/papaparse.js index 18715b8..4accc0a 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1266,7 +1266,7 @@ if (!Array.isArray) if (typeof fieldCountPrevRow === 'undefined') { - fieldCountPrevRow = fieldCount; + fieldCountPrevRow = 0; continue; } else if (fieldCount > 1) @@ -1279,7 +1279,7 @@ if (!Array.isArray) if (preview.data.length > 0) avgFieldCount /= (preview.data.length - emptyLinesCount); - if ((typeof bestDelta === 'undefined' || delta < bestDelta) + if ((typeof bestDelta === 'undefined' || delta > bestDelta) && avgFieldCount > 1.99) { bestDelta = delta; diff --git a/tests/test-cases.js b/tests/test-cases.js index b596568..5f4c6ee 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1159,6 +1159,16 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Pipe delimiter is guessed correctly when mixed with comas", + notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file", + input: 'one|two,two|three\nfour|five,five|six', + config: {}, + expected: { + data: [['one','two,two','three'],['four','five,five','six']], + errors: [] + } + }, { description: "Single quote as quote character", notes: "Must parse correctly when single quote is specified as a quote character",