diff --git a/papaparse.js b/papaparse.js index 704f186..4968fa0 100644 --- a/papaparse.js +++ b/papaparse.js @@ -876,9 +876,9 @@ _config.newline = guessLineEndings(input); _delimiterError = false; - if (!_config.delimiter) + if (!_config.delimiter || isArray(_config.delimiter)) { - var delimGuess = guessDelimiter(input, _config.newline); + var delimGuess = guessDelimiter(input, _config.newline, _config.delimiter); if (delimGuess.successful) _config.delimiter = delimGuess.bestDelimiter; else @@ -1040,10 +1040,14 @@ return _results; } - function guessDelimiter(input, newline) + function guessDelimiter(input, newline, customDelimChoices) { - var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; - var bestDelim, bestDelta, fieldCountPrevRow; + var bestDelim, bestDelta, delimChoices, fieldCountPrevRow; + + if (!customDelimChoices) + delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; + else + delimChoices = customDelimChoices; for (var i = 0; i < delimChoices.length; i++) { @@ -1566,5 +1570,10 @@ return typeof func === 'function'; } + function isArray(arr) + { + return Object.prototype.toString.call(arr) === '[object Array]'; + } + return Papa; })); diff --git a/tests/test-cases.js b/tests/test-cases.js index 5ae3afb..8abf098 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -634,6 +634,24 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Custom array of potential delimiters", + input: "a\t,b\t,c\t", + config: { delimiter: ['|', ';', ','] }, + expected: { + data: [["a\t", "b\t", "c\t"]], + errors: [] + } + }, + { + description: "Custom delimiter array, correctly guesses most prevalent one", + input: 'a;%b,%c,%d;', + config: { delimiter: ['%', ',', ';'] }, + expected: { + data: [['a;', 'b,', 'c,', 'd;']], + errors: [] + } + }, { description: "Dynamic typing converts numeric literals", input: '1,2.2,1e3\r\n-4,-4.5,-4e-5\r\n-,5a,5-2',