Browse Source

- fixes multi-character delimiter with quoted field issue (#879)

pull/836/merge
janisdd 4 years ago committed by GitHub
parent
commit
23e1b47f5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      docs/docs.html
  2. 2
      papaparse.js
  3. 16
      tests/test-cases.js

6
docs/docs.html

@ -303,7 +303,7 @@
<code>delimiter</code> <code>delimiter</code>
</td> </td>
<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> </td>
</tr> </tr>
<tr> <tr>
@ -470,7 +470,7 @@ var csv = Papa.unparse({
<code>delimiter</code> <code>delimiter</code>
</td> </td>
<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> </td>
</tr> </tr>
<tr> <tr>
@ -861,7 +861,7 @@ var csv = Papa.unparse({
<tr> <tr>
<td><code>Papa.BAD_DELIMITERS</code></td> <td><code>Papa.BAD_DELIMITERS</code></td>
<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> </td>
</tr> </tr>
<tr> <tr>

2
papaparse.js

@ -1555,7 +1555,7 @@ License: MIT
var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo); var spacesBetweenQuoteAndDelimiter = extraSpaces(checkUpTo);
// Closing quote followed by delimiter or 'unnecessary spaces + delimiter' // 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)); row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen; cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;

16
tests/test-cases.js

@ -847,6 +847,16 @@ var PARSE_TESTS = [
errors: [] 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", description: "Callback delimiter",
input: 'a$ b$ c', input: 'a$ b$ c',
@ -1713,6 +1723,12 @@ var UNPARSE_TESTS = [
config: { delimiter: ', ' }, config: { delimiter: ', ' },
expected: 'A, b, c\r\nd, e, f' 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)", description: "Bad delimiter (\\n)",
notes: "Should default to comma", notes: "Should default to comma",

Loading…
Cancel
Save