Repository: mholt/PapaParse
Pull request #687 (2019-06-27 16:03:56)

Modified the GuessDelimiter function
question: function return different cause the version change

input:"a,b,c
a,b,c|d|e|f"

output:[["a", "b", "c"], ["a", "b", "c|d|e|f"]] in v4
              [["a,b,c"],["a,b,c","d","e","f",]] in v5

expect :[["a", "b", "c"], ["a", "b", "c|d|e|f"]]

resolve:  delta should  record the difference between line which mean smaller is better, when delta is equal, we should compare the avgfieldcount and choose the biggest one.by the way the issue592 can resolve right array use this function. 

pokoli: Hi @morance, 

First of all thank you so much for this pull request. 

Could you please add the example on your description to [our test suite](https://github.com/mholt/PapaParse/blob/master/tests/test-cases.js). 
So this way we ensure this behaviour stays consistent in the next versions. 

Once added I will be very happy to merge it. 

pokoli: I think this will solve #675 
morance: ok, I add the test suite already. hope you can adopt it Thanks

Patch:
diff --git a/papaparse.js b/papaparse.js
index ac02f6d..3a1f313 100755
--- a/papaparse.js
+++ b/papaparse.js
@@ -1261,14 +1261,12 @@ License: MIT
 			return _results;
 		}
 
-		function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess)
-		{
-			var bestDelim, bestDelta, fieldCountPrevRow;
+		function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) {
+			var bestDelim, bestDelta, fieldCountPrevRow, maxFieldCount;
 
 			delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
 
-			for (var i = 0; i < delimitersToGuess.length; i++)
-			{
+			for (var i = 0; i < delimitersToGuess.length; i++) {
 				var delim = delimitersToGuess[i];
 				var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
 				fieldCountPrevRow = undefined;
@@ -1280,23 +1278,19 @@ License: MIT
 					preview: 10
 				}).parse(input);
 
-				for (var j = 0; j < preview.data.length; j++)
-				{
-					if (skipEmptyLines && testEmptyLine(preview.data[j]))
-					{
+				for (var j = 0; j < preview.data.length; j++) {
+					if (skipEmptyLines && testEmptyLine(preview.data[j])) {
 						emptyLinesCount++;
 						continue;
 					}
 					var fieldCount = preview.data[j].length;
 					avgFieldCount += fieldCount;
 
-					if (typeof fieldCountPrevRow === 'undefined')
-					{
-						fieldCountPrevRow = 0;
+					if (typeof fieldCountPrevRow === 'undefined') {
+						fieldCountPrevRow = fieldCount;
 						continue;
 					}
-					else if (fieldCount > 1)
-					{
+					else if (fieldCount > 0) {
 						delta += Math.abs(fieldCount - fieldCountPrevRow);
 						fieldCountPrevRow = fieldCount;
 					}
@@ -1305,11 +1299,11 @@ License: MIT
 				if (preview.data.length > 0)
 					avgFieldCount /= (preview.data.length - emptyLinesCount);
 
-				if ((typeof bestDelta === 'undefined' || delta > bestDelta)
-					&& avgFieldCount > 1.99)
-				{
+				if ((typeof bestDelta === 'undefined' || delta <= bestDelta)
+					&& (typeof maxFieldCount === 'undefined' || avgFieldCount > maxFieldCount) && avgFieldCount > 1.99) {
 					bestDelta = delta;
 					bestDelim = delim;
+					maxFieldCount = avgFieldCount;
 				}
 			}
 


Test patch:
diff --git a/tests/test-cases.js b/tests/test-cases.js
index eef1862..9d5be3a 100644
--- a/tests/test-cases.js
+++ b/tests/test-cases.js
@@ -1203,6 +1203,16 @@ var PARSE_TESTS = [
 			errors: []
 		}
 	},
+	{
+		description: "Pipe delimiter is guessed correctly choose avgFildCount max one",
+		notes: "Guessing the delimiter should work choose the min delta one and the max one",
+		config: {},
+		input: 'a,b,c\na,b,c|d|e|f',
+		expected: {
+			data: [['a', 'b', 'c'], ['a','b','c|d|e|f']],
+			errors: []
+		}
+	},
 	{
 		description: "Pipe delimiter is guessed correctly when first field are enclosed in quotes and contain delimiter characters",
 		notes: "Guessing the delimiter should work if the first field is enclosed in quotes, but others are not",