diff --git a/papaparse.js b/papaparse.js index d0dbef1..618d58d 100644 --- a/papaparse.js +++ b/papaparse.js @@ -243,8 +243,13 @@ /** newline character(s) */ var _newline = '\r\n'; + /** quote character */ + var _quoteChar = '"'; + unpackConfig(); + var quoteCharRegex = new RegExp(_quoteChar, 'g'); + if (typeof _input === 'string') _input = JSON.parse(_input); @@ -300,6 +305,9 @@ if (typeof _config.newline === 'string') _newline = _config.newline; + if (typeof _config.quoteChar === 'string') + _quoteChar = _config.quoteChar; + if (typeof _config.header === 'boolean') _writeHeader = _config.header; } @@ -368,7 +376,7 @@ if (typeof str === 'undefined' || str === null) return ''; - str = str.toString().replace(/"/g, '""'); + str = str.toString().replace(quoteCharRegex, _quoteChar+_quoteChar); var needsQuotes = (typeof _quotes === 'boolean' && _quotes) || (_quotes instanceof Array && _quotes[col]) @@ -377,7 +385,7 @@ || str.charAt(0) === ' ' || str.charAt(str.length - 1) === ' '; - return needsQuotes ? '"' + str + '"' : str; + return needsQuotes ? _quoteChar + str + _quoteChar : str; } function hasAny(str, substrings) diff --git a/tests/test-cases.js b/tests/test-cases.js index 677ba82..e31571c 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -901,16 +901,16 @@ var PARSE_TESTS = [ errors: [] } }, - { - description: "Single quote as quote character", - notes: "Must parse correctly when single quote is specified as a quote character", - input: "a,b,'c,d'", - config: { quoteChar: "'"}, - expected: { - data: [['a', 'b', 'c,d']], - errors: [] - } - } + { + description: "Single quote as quote character", + notes: "Must parse correctly when single quote is specified as a quote character", + input: "a,b,'c,d'", + config: { quoteChar: "'"}, + expected: { + data: [['a', 'b', 'c,d']], + errors: [] + } + } ]; describe('Parse Tests', function() { @@ -1163,6 +1163,12 @@ var UNPARSE_TESTS = [ expected: 'Col1,Col2,Col3\r\na,,c' }, { + description: "Custom quote character (single quote)", + input: [['a,d','b','c']], + config: { quoteChar: "'"}, + expected: "'a,d',b,c" + }, + { description: "Don't print header if header:false option specified", input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": "e", "Col3": "f" }], config: { header: false },