Browse Source

Add ability to support escapeChar on unparse (#631)

pull/647/head
Ledion Bitincka 6 years ago committed by Sergi Almacellas Abellana
parent
commit
a627547d87
  1. 7
      docs/docs.html
  2. 9
      papaparse.js
  3. 12
      tests/test-cases.js

7
docs/docs.html

@ -258,6 +258,7 @@ @@ -258,6 +258,7 @@
{
quotes: false, //or array of booleans
quoteChar: '"',
escapeChar: '"',
delimiter: ",",
header: true,
newline: "\r\n",
@ -293,6 +294,12 @@ @@ -293,6 +294,12 @@
The character used to quote fields.
</td>
</tr>
<tr>
<td><code>escapeChar</code></td>
<td>
The character used to escape <code>quoteChar</code> inside field values.
</td>
</tr>
<tr>
<td>
<code>delimiter</code>

9
papaparse.js

@ -273,6 +273,9 @@ License: MIT @@ -273,6 +273,9 @@ License: MIT
/** quote character */
var _quoteChar = '"';
/** escaped quote character, either "" or <config.escapeChar>" */
var _escapedQuote = _quoteChar + _quoteChar;
/** whether to skip empty lines */
var _skipEmptyLines = false;
@ -353,6 +356,10 @@ License: MIT @@ -353,6 +356,10 @@ License: MIT
_columns = _config.columns;
}
if (_config.escapeChar !== undefined) {
_escapedQuote = _config.escapeChar + _quoteChar;
}
}
@ -439,7 +446,7 @@ License: MIT @@ -439,7 +446,7 @@ License: MIT
if (str.constructor === Date)
return JSON.stringify(str).slice(1, 25);
str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar);
str = str.toString().replace(quoteCharRegex, _escapedQuote);
var needsQuotes = (typeof _quotes === 'boolean' && _quotes)
|| (Array.isArray(_quotes) && _quotes[col])

12
tests/test-cases.js

@ -1739,6 +1739,18 @@ var UNPARSE_TESTS = [ @@ -1739,6 +1739,18 @@ var UNPARSE_TESTS = [
input: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}],
config: {columns: ['a', 'b', 'c']},
expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4'
},
{
description: "Use different escapeChar",
input: [{a: 'foo', b: '"quoted"'}],
config: {header: false, escapeChar: '\\'},
expected: 'foo,"\\"quoted\\""'
},
{
description: "test defeault escapeChar",
input: [{a: 'foo', b: '"quoted"'}],
config: {header: false},
expected: 'foo,"""quoted"""'
}
];

Loading…
Cancel
Save