Browse Source

Fixed #532 - Implemented onlyQuoteStrings

pull/534/head
Dennis Boldt 7 years ago
parent
commit
98506830e9
  1. 16
      papaparse.js
  2. 12
      tests/test-cases.js

16
papaparse.js

@ -280,6 +280,9 @@
/** quote character */ /** quote character */
var _quoteChar = '"'; var _quoteChar = '"';
/** quote character */
var _onlyQuoteStrings = false;
unpackConfig(); unpackConfig();
var quoteCharRegex = new RegExp(_quoteChar, 'g'); var quoteCharRegex = new RegExp(_quoteChar, 'g');
@ -343,6 +346,11 @@
if (typeof _config.header === 'boolean') if (typeof _config.header === 'boolean')
_writeHeader = _config.header; _writeHeader = _config.header;
if (typeof _config.onlyQuoteStrings === 'boolean') {
_onlyQuoteStrings = _config.onlyQuoteStrings;
_quotes = false; // Override quotes, since only quote strings is selected
}
} }
@ -412,9 +420,15 @@
if (str.constructor === Date) if (str.constructor === Date)
return JSON.stringify(str).slice(1, 25); return JSON.stringify(str).slice(1, 25);
// We must check, whether the data type is string already here,
// since all data types are converted into strings in the next line
var onlyQuoteStrings = _onlyQuoteStrings && typeof str === 'string';
// Converts every data type to string
str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar);
var needsQuotes = (typeof _quotes === 'boolean' && _quotes) var needsQuotes = onlyQuoteStrings
|| (typeof _quotes === 'boolean' && _quotes)
|| (_quotes instanceof Array && _quotes[col]) || (_quotes instanceof Array && _quotes[col])
|| hasAny(str, Papa.BAD_DELIMITERS) || hasAny(str, Papa.BAD_DELIMITERS)
|| str.indexOf(_delimiter) > -1 || str.indexOf(_delimiter) > -1

12
tests/test-cases.js

@ -1451,6 +1451,18 @@ var UNPARSE_TESTS = [
config: { quotes: [true, false, true] }, config: { quotes: [true, false, true] },
expected: '"Col1",Col2,"Col3"\r\n"a",b,"c"\r\n"d",e,"f"' expected: '"Col1",Col2,"Col3"\r\n"a",b,"c"\r\n"d",e,"f"'
}, },
{
description: "Force quotes around string fields only",
input: [['a', 'b', 'c'], ['d', 10, true]],
config: { onlyQuoteStrings: true },
expected: '"a","b","c"\r\n"d",10,true'
},
{
description: "Force quotes around string fields only (with header row)",
input: [{ "Col1": "a", "Col2": "b", "Col3": "c" }, { "Col1": "d", "Col2": 10, "Col3": true }],
config: { onlyQuoteStrings: true },
expected: '"Col1","Col2","Col3"\r\n"a","b","c"\r\n"d",10,true'
},
{ {
description: "Empty input", description: "Empty input",
input: [], input: [],

Loading…
Cancel
Save