Skip to content

Commit

Permalink
- fixes multi-character delimiter with quoted field issue (mholt#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
janisdd authored Jun 18, 2021
1 parent a6fdfcb commit 23e1b47
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ <h5>Unparse Config Options</h5>
<code>delimiter</code>
</td>
<td>
The delimiting character. It must not be found in <a href="#readonly">Papa.BAD_DELIMITERS</a>.
The delimiting character. Multi-character delimiters are supported. It must not be found in <a href="#readonly">Papa.BAD_DELIMITERS</a>.
</td>
</tr>
<tr>
Expand Down Expand Up @@ -470,7 +470,7 @@ <h5 id="config-details">Config Options</h5>
<code>delimiter</code>
</td>
<td>
The delimiting character. Leave blank to auto-detect from a list of most common delimiters, or any values passed in through <code>delimitersToGuess</code>. 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 <a href="#readonly">Papa.BAD_DELIMITERS</a>.
The delimiting character. Leave blank to auto-detect from a list of most common delimiters, or any values passed in through <code>delimitersToGuess</code>. 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 <a href="#readonly">Papa.BAD_DELIMITERS</a>.
</td>
</tr>
<tr>
Expand Down Expand Up @@ -861,7 +861,7 @@ <h5 id="readonly">Read-Only</h5>
<tr>
<td><code>Papa.BAD_DELIMITERS</code></td>
<td>
An array of characters that are not allowed as delimiters.
An array of characters that are not allowed as delimiters (<code>\r, \n, ", \ufeff</code>).
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion papaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 23e1b47

Please sign in to comment.