diff --git a/docs/docs.html b/docs/docs.html index 68f1132..3749641 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -247,13 +247,37 @@
// defaults shown
 {
 	quotes: false,
+	onlyQuoteStrings: false,
 	quoteChar: '"',
 	escapeChar: '"',
 	delimiter: ",",
-	header: true,
-	newline: "\r\n"
+	newline: "\r\n",
+	header: true
 }
- Set quotes to true to always enclose each field in quotes, or an array of true/false values correlating to specific to columns to force-quote. The character used to quote can be customized using quoteChar. The character used to escape the quoteChar within a field can be customized using escapeChar. The delimiter can be any valid delimiting character. The newline character(s) may also be customized. Setting header to false will omit the header row. + + diff --git a/papaparse.js b/papaparse.js index 67f5e2e..2bb0ebc 100755 --- a/papaparse.js +++ b/papaparse.js @@ -280,6 +280,9 @@ /** quote character */ var _quoteChar = '"'; + /** whether only strings should be quoted */ + var _onlyQuoteStrings = false; + unpackConfig(); var quoteCharRegex = new RegExp(_quoteChar, 'g'); @@ -343,6 +346,11 @@ if (typeof _config.header === 'boolean') _writeHeader = _config.header; + + if (typeof _config.onlyQuoteStrings === 'boolean') { + _onlyQuoteStrings = _config.onlyQuoteStrings; + _quotes = false; // Override quotes, since only quote strings is selected + } } @@ -404,17 +412,23 @@ } /** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */ - function safe(str, col) + function safe(value, col) { - if (typeof str === 'undefined' || str === null) + if (typeof value === 'undefined' || value === null) return ''; - if (str.constructor === Date) - return JSON.stringify(str).slice(1, 25); + if (value.constructor === Date) + return JSON.stringify(value).slice(1, 25); + + // We must check, whether the data type of value is string already here, + // since all data types are converted into strings in the next line + var onlyQuoteStrings = _onlyQuoteStrings && typeof value === 'string'; - str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); + // Converts every data type to string + var str = value.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); - var needsQuotes = (typeof _quotes === 'boolean' && _quotes) + var needsQuotes = onlyQuoteStrings + || (typeof _quotes === 'boolean' && _quotes) || (_quotes instanceof Array && _quotes[col]) || hasAny(str, Papa.BAD_DELIMITERS) || str.indexOf(_delimiter) > -1 diff --git a/tests/test-cases.js b/tests/test-cases.js index be5cc5a..276a9cf 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1605,6 +1605,18 @@ var UNPARSE_TESTS = [ config: { quotes: [true, false, true] }, 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", input: [],