Browse Source

Add Custom Quote Character to unparse (#329)

pull/325/merge
Prayash Mohapatra 8 years ago committed by Sergi Almacellas Abellana
parent
commit
32cc6cad64
  1. 12
      papaparse.js
  2. 26
      tests/test-cases.js

12
papaparse.js

@ -243,8 +243,13 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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)

26
tests/test-cases.js

@ -901,16 +901,16 @@ var PARSE_TESTS = [ @@ -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 = [ @@ -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 },

Loading…
Cancel
Save