From 739476ecdff4b210cb85cae724dc31db2be3a8c1 Mon Sep 17 00:00:00 2001 From: Eric Fossas Date: Mon, 7 May 2018 01:48:05 -0600 Subject: [PATCH] Ignore lines with comments when guessing delimiter (#503) --- papaparse.js | 5 +++-- tests/test-cases.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/papaparse.js b/papaparse.js index 4ee8de2..fa8611d 100755 --- a/papaparse.js +++ b/papaparse.js @@ -898,7 +898,7 @@ _delimiterError = false; if (!_config.delimiter) { - var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines); + var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments); if (delimGuess.successful) _config.delimiter = delimGuess.bestDelimiter; else @@ -1073,7 +1073,7 @@ return _results; } - function guessDelimiter(input, newline, skipEmptyLines) + function guessDelimiter(input, newline, skipEmptyLines, comments) { var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; var bestDelim, bestDelta, fieldCountPrevRow; @@ -1085,6 +1085,7 @@ fieldCountPrevRow = undefined; var preview = new Parser({ + comments: comments, delimiter: delim, newline: newline, preview: 10 diff --git a/tests/test-cases.js b/tests/test-cases.js index 773635e..b10c386 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1090,6 +1090,26 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Lines with comments are not used when guessing the delimiter in an escaped file", + notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file", + input: '#1\n#2\n#3\n#4\n#5\n#6\n#7\n#8\n#9\n#10\none,"t,w,o",three\nfour,five,six', + config: { comments: '#' }, + expected: { + data: [['one','t,w,o','three'],['four','five','six']], + errors: [] + } + }, + { + description: "Lines with comments are not used when guessing the delimiter in a non-escaped file", + notes: "Guessing the delimiter should work even if there are many lines of comments at the start of the file", + input: '#1\n#2\n#3\n#4\n#5\n#6\n#7\n#8\n#9\n#10\n#11\none,two,three\nfour,five,six', + config: { comments: '#' }, + expected: { + data: [['one','two','three'],['four','five','six']], + errors: [] + } + }, { description: "Single quote as quote character", notes: "Must parse correctly when single quote is specified as a quote character",