From 757b1bf6e04353b7e82a960e4fd9936495cd4e3c Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Mon, 4 Feb 2019 09:45:35 +0100 Subject: [PATCH] Add DelimitersToGuess config option (#555) --- docs/docs.html | 13 +++++++++++-- papaparse.js | 11 ++++++----- tests/test-cases.js | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/docs/docs.html b/docs/docs.html index 2c31312..59c87d5 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -355,7 +355,8 @@ var csv = Papa.unparse({ fastMode: undefined, beforeFirstChunk: undefined, withCredentials: undefined, - transform: undefined + transform: undefined, + delimitersToGuess: [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP] }
@@ -375,7 +376,7 @@ var csv = Papa.unparse({ delimiter - The delimiting character. Leave blank to auto-detect from a list of most common delimiters. It can be a string or a function. If string, it must be one of length 1. If a function, it must accept the input as first parameter and it must return a string which will be used as delimiter. In both cases it cannot be found in Papa.BAD_DELIMITERS. + The delimiting character. Leave blank to auto-detect from a list of most common delimiters, or any values passed in through delimitersToGuess. It can be a string or a function. If string, it must be one of length 1. If a function, it must accept the input as first parameter and it must return a string which will be used as delimiter. In both cases it cannot be found in Papa.BAD_DELIMITERS. @@ -549,6 +550,14 @@ var csv = Papa.unparse({ A function to apply on each value. The function receives the value as its first argument and the column number or header name when enabled as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping. + + + delimitersToGuess + + + An array of delimiters to guess from if the delimiter option is not set. + + diff --git a/papaparse.js b/papaparse.js index 6f18efe..e0ddca9 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1031,7 +1031,7 @@ License: MIT _delimiterError = false; if (!_config.delimiter) { - var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments); + var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines, _config.comments, _config.delimitersToGuess); if (delimGuess.successful) _config.delimiter = delimGuess.bestDelimiter; else @@ -1215,14 +1215,15 @@ License: MIT return _results; } - function guessDelimiter(input, newline, skipEmptyLines, comments) + function guessDelimiter(input, newline, skipEmptyLines, comments, delimitersToGuess) { - var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; var bestDelim, bestDelta, fieldCountPrevRow; - for (var i = 0; i < delimChoices.length; i++) + delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; + + for (var i = 0; i < delimitersToGuess.length; i++) { - var delim = delimChoices[i]; + var delim = delimitersToGuess[i]; var delta = 0, avgFieldCount = 0, emptyLinesCount = 0; fieldCountPrevRow = undefined; diff --git a/tests/test-cases.js b/tests/test-cases.js index e8b424f..72338f1 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -2188,7 +2188,6 @@ var CUSTOM_TESTS = [ }); } } - ]; describe('Custom Tests', function() { @@ -2205,3 +2204,40 @@ describe('Custom Tests', function() { generateTest(CUSTOM_TESTS[i]); } }); + + +var DELIMITERS_TO_GUESS_TESTS = [ + { + description: "Should correctly guess custom delimiter when passed delimiters to guess.", + expected: "~", + run: function(callback) { + var results = Papa.parse('"A"~"B"~"C"~"D"', { + delimitersToGuess: ['~', '@', '%'] + }); + callback(results.meta.delimiter); + } + }, + { + description: "Should still correctly guess default delimiters when delimiters to guess are not given.", + expected: ",", + run: function(callback) { + var results = Papa.parse('"A","B","C","D"'); + callback(results.meta.delimiter); + } + } +]; + +describe('Delimiters to Guess Tests', function() { + function generateTest(test) { + (test.disabled ? it.skip : it)(test.description, function(done) { + test.run(function(actual) { + assert.deepEqual(JSON.stringify(actual), JSON.stringify(test.expected)); + done(); + }); + }); + } + + for (var i = 0; i < DELIMITERS_TO_GUESS_TESTS.length; i++) { + generateTest(DELIMITERS_TO_GUESS_TESTS[i]); + } +});