Skip to content

Commit

Permalink
Modified the GuessDelimiter function (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
morance authored and pokoli committed Jun 29, 2019
1 parent 54f8aec commit 792641e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
28 changes: 11 additions & 17 deletions papaparse.js
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/test-cases.js
Expand Up @@ -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",
Expand Down

0 comments on commit 792641e

Please sign in to comment.