From 23e1b47f5c1ebcd4b06f696fc555563a18d6b74b Mon Sep 17 00:00:00 2001 From: janisdd Date: Fri, 18 Jun 2021 14:13:13 +0200 Subject: [PATCH] - fixes multi-character delimiter with quoted field issue (#879) --- docs/docs.html | 6 +++--- papaparse.js | 2 +- tests/test-cases.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/docs.html b/docs/docs.html index da3c8b6..c2d82bd 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -303,7 +303,7 @@ delimiter - The delimiting character. It must not be found in Papa.BAD_DELIMITERS. + The delimiting character. Multi-character delimiters are supported. It must not be found in Papa.BAD_DELIMITERS. @@ -470,7 +470,7 @@ var csv = Papa.unparse({ delimiter - 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. + 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 a string, it can be of any length (so multi-character delimiters are supported). 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. @@ -861,7 +861,7 @@ var csv = Papa.unparse({ Papa.BAD_DELIMITERS - An array of characters that are not allowed as delimiters. + An array of characters that are not allowed as delimiters (\r, \n, ", \ufeff). diff --git a/papaparse.js b/papaparse.js index 090c663..057a16c 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1555,7 +1555,7 @@ License: MIT var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo); // Closing quote followed by delimiter or 'unnecessary spaces + delimiter' - if (input[quoteSearch + 1 + spacesBetweenQuoteAndDelimiter] === delim) + if (input.substr(quoteSearch + 1 + spacesBetweenQuoteAndDelimiter, delimLen) === delim) { row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen; diff --git a/tests/test-cases.js b/tests/test-cases.js index d727bfe..0c3a62d 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -847,6 +847,16 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Multi-character delimiter (length 2) with quoted field", + input: 'a, b, "c, e", d', + config: { delimiter: ", " }, + notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field", + expected: { + data: [['a', 'b', 'c, e', 'd']], + errors: [] + } + }, { description: "Callback delimiter", input: 'a$ b$ c', @@ -1713,6 +1723,12 @@ var UNPARSE_TESTS = [ config: { delimiter: ', ' }, expected: 'A, b, c\r\nd, e, f' }, + { + description: "Custom delimiter (Multi-character), field contains custom delimiter", + input: [['A', 'b', 'c'], ['d', 'e', 'f, g']], + config: { delimiter: ', ' }, + expected: 'A, b, c\r\nd, e, "f, g"' + }, { description: "Bad delimiter (\\n)", notes: "Should default to comma",