Browse Source

Ignore lines with comments when guessing delimiter (#503)

pull/509/head
Eric Fossas 7 years ago committed by Sergi Almacellas Abellana
parent
commit
739476ecdf
  1. 5
      papaparse.js
  2. 20
      tests/test-cases.js

5
papaparse.js

@ -898,7 +898,7 @@
_delimiterError = false; _delimiterError = false;
if (!_config.delimiter) if (!_config.delimiter)
{ {
var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines); var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments);
if (delimGuess.successful) if (delimGuess.successful)
_config.delimiter = delimGuess.bestDelimiter; _config.delimiter = delimGuess.bestDelimiter;
else else
@ -1073,7 +1073,7 @@
return _results; return _results;
} }
function guessDelimiter(input, newline, skipEmptyLines) function guessDelimiter(input, newline, skipEmptyLines, comments)
{ {
var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
var bestDelim, bestDelta, fieldCountPrevRow; var bestDelim, bestDelta, fieldCountPrevRow;
@ -1085,6 +1085,7 @@
fieldCountPrevRow = undefined; fieldCountPrevRow = undefined;
var preview = new Parser({ var preview = new Parser({
comments: comments,
delimiter: delim, delimiter: delim,
newline: newline, newline: newline,
preview: 10 preview: 10

20
tests/test-cases.js

@ -1090,6 +1090,26 @@ var PARSE_TESTS = [
errors: [] 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", description: "Single quote as quote character",
notes: "Must parse correctly when single quote is specified as a quote character", notes: "Must parse correctly when single quote is specified as a quote character",

Loading…
Cancel
Save