Browse Source

add ability to support escapeChar on unparse

pull/631/head
Ledion Bitincka 6 years ago
parent
commit
125796cc46
  1. 9
      papaparse.js
  2. 6
      tests/test-cases.js

9
papaparse.js

@ -273,6 +273,9 @@ License: MIT
/** quote character */ /** quote character */
var _quoteChar = '"'; var _quoteChar = '"';
/** escaped quote character, either "" or <config.escapeChar>" */
var _escapedQuote = _quoteChar + _quoteChar;
/** whether to skip empty lines */ /** whether to skip empty lines */
var _skipEmptyLines = false; var _skipEmptyLines = false;
@ -343,6 +346,10 @@ License: MIT
if (typeof _config.header === 'boolean') if (typeof _config.header === 'boolean')
_writeHeader = _config.header; _writeHeader = _config.header;
if (_config.escapeChar !== undefined) {
_escapedQuote = _config.escapeChar + _quoteChar;
}
} }
@ -429,7 +436,7 @@ License: MIT
if (str.constructor === Date) if (str.constructor === Date)
return JSON.stringify(str).slice(1, 25); 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) var needsQuotes = (typeof _quotes === 'boolean' && _quotes)
|| (Array.isArray(_quotes) && _quotes[col]) || (Array.isArray(_quotes) && _quotes[col])

6
tests/test-cases.js

@ -1732,6 +1732,12 @@ var UNPARSE_TESTS = [
input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}], input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}],
config: {skipEmptyLines: 'greedy', header: true}, config: {skipEmptyLines: 'greedy', header: true},
expected: 'a,b\r\n1,2' expected: 'a,b\r\n1,2'
},
{
description: "Use different escapeChar",
input: [{a: 'foo', b: '"quoted"'}],
config: {header: false, escapeChar: '\\'},
expected: 'foo,"\\"quoted\\""'
} }
]; ];

Loading…
Cancel
Save