From 98506830e98edfe32caa7e363a926ebf2d92d1da Mon Sep 17 00:00:00 2001 From: Dennis Boldt <boldt@itm.uni-luebeck.de> Date: Fri, 6 Jul 2018 14:03:27 +0200 Subject: [PATCH 1/5] Fixed #532 - Implemented onlyQuoteStrings --- papaparse.js | 16 +++++++++++++++- tests/test-cases.js | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/papaparse.js b/papaparse.js index 5f670bd7..4b245b7e 100755 --- a/papaparse.js +++ b/papaparse.js @@ -280,6 +280,9 @@ /** quote character */ var _quoteChar = '"'; + /** quote character */ + 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 + } } @@ -412,9 +420,15 @@ if (str.constructor === Date) 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); - 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 ad0d9a58..178e3156 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1451,6 +1451,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: [], From 1d247a21db9516c120038fa21d9143a77278d25d Mon Sep 17 00:00:00 2001 From: Dennis Boldt <info@dennis-boldt.de> Date: Fri, 6 Jul 2018 23:48:22 +0200 Subject: [PATCH 2/5] Refactor variable srt to value --- papaparse.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/papaparse.js b/papaparse.js index 4b245b7e..1b845d11 100755 --- a/papaparse.js +++ b/papaparse.js @@ -280,7 +280,7 @@ /** quote character */ var _quoteChar = '"'; - /** quote character */ + /** whether only strings should be quoted */ var _onlyQuoteStrings = false; unpackConfig(); @@ -412,20 +412,20 @@ } /** 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 is string already here, + // 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 str === 'string'; + var onlyQuoteStrings = _onlyQuoteStrings && typeof value === 'string'; // Converts every data type to string - str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); + var str = value.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); var needsQuotes = onlyQuoteStrings || (typeof _quotes === 'boolean' && _quotes) From 2c9b709f7416742555c85a9565d7bafd09ca7526 Mon Sep 17 00:00:00 2001 From: Dennis Boldt <info@dennis-boldt.de> Date: Sat, 7 Jul 2018 00:04:25 +0200 Subject: [PATCH 3/5] Added doc for the onlyQuoteStrings property --- docs/docs.html | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/docs/docs.html b/docs/docs.html index 37fe8f1c..973e3909 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -7,12 +7,12 @@ <meta name="theme-color" content="#ffffff"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Arvo|Source+Sans+Pro:400,400italic,700|Lato:300,400"> - <link rel="stylesheet" href="/resources/css/unsemantic.css"> - <link rel="stylesheet" href="/resources/css/tomorrow.highlight.css"> - <link rel="stylesheet" href="/resources/css/common.css"> - <script src="/resources/js/jquery.min.js"></script> - <script src="/resources/js/highlight.min.js"></script> - <script src="/resources/js/common.js"></script> + <link rel="stylesheet" href="resources/css/unsemantic.css"> + <link rel="stylesheet" href="resources/css/tomorrow.highlight.css"> + <link rel="stylesheet" href="resources/css/common.css"> + <script src="resources/js/jquery.min.js"></script> + <script src="resources/js/highlight.min.js"></script> + <script src="resources/js/common.js"></script> <script>hljs.initHighlightingOnLoad();</script> </head> <body> @@ -247,13 +247,37 @@ <h5 id="unparse">Unparse</h5> <pre><code class="language-javascript">// defaults shown { quotes: false, + onlyQuoteStrings: false, quoteChar: '"', escapeChar: '"', delimiter: ",", - header: true, - newline: "\r\n" + newline: "\r\n", + header: true }</code></pre> - Set <code>quotes</code> to <code>true</code> 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 <code>quoteChar</code>. The character used to escape the <code>quoteChar</code> within a field can be customized using <code>escapeChar</code>. The <code>delimiter</code> can be any valid delimiting character. The <code>newline</code> character(s) may also be customized. Setting <code>header</code> to <code>false</code> will omit the header row. + <ul> + <li> + <code>quotes</code>: Set this property to <code>true</code> to always enclose each field in quotes, or an array of true/false values correlating to specific to columns to force-quote (e.g., <code>[true, false, true]</code>). + </li> + <li> + <code>onlyQuoteStrings</code>: Setting this property to <code>true</code> will just quote strings in the resulting CSV. All other data types like numbers or booleans will not be quoted. Note, that this property overrides the <code>quotes</code> property. + </li> + <li> + <code>quoteChar</code>: The character used to quote. + </li> + <li> + <code>escapeChar</code>: The character used to escape the <code>quoteChar</code> within a field. + </li> + <li> + <code>delimiter</code>: This property can be any valid delimiting character. + </li> + <li> + <code>newline</code>: This property can be any valid newline character(s). + </li> + <li> + <code>header</code>: Setting this property to <code>false</code> will omit the header row. + </li> + </ul> + </li> </ul> </div> @@ -803,7 +827,7 @@ <h5 id="configurable">Configurable</h5> <footer> <!--<div class="footer-top"> <h3>Make Your Papa Proud</h3> - <h4><a href="https://github.com/mholt/PapaParse">Star</a> and <a href="https://github.com/mholt/PapaParse/blob/gh-pages/resources/js/lovers.js">shout</a> if you love #PapaParse</h4> + <h4><a href="https://github.com/mholt/PapaParse">Star</a> and <a href="https://github.com/mholt/PapaParse/blob/gh-pagesresources/js/lovers.js">shout</a> if you love #PapaParse</h4> </div>--> <div class="footer-main"> <div class="grid-container"> From 1c06fa7efccbef262e5eca42c1032594dcf7ec5b Mon Sep 17 00:00:00 2001 From: Dennis Boldt <info@dennis-boldt.de> Date: Sat, 7 Jul 2018 00:18:43 +0200 Subject: [PATCH 4/5] Undo local test environment changes. --- docs/docs.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/docs.html b/docs/docs.html index 973e3909..93a3757b 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -7,12 +7,12 @@ <meta name="theme-color" content="#ffffff"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"> <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Arvo|Source+Sans+Pro:400,400italic,700|Lato:300,400"> - <link rel="stylesheet" href="resources/css/unsemantic.css"> - <link rel="stylesheet" href="resources/css/tomorrow.highlight.css"> - <link rel="stylesheet" href="resources/css/common.css"> - <script src="resources/js/jquery.min.js"></script> - <script src="resources/js/highlight.min.js"></script> - <script src="resources/js/common.js"></script> + <link rel="stylesheet" href="/resources/css/unsemantic.css"> + <link rel="stylesheet" href="/resources/css/tomorrow.highlight.css"> + <link rel="stylesheet" href="/resources/css/common.css"> + <script src="/resources/js/jquery.min.js"></script> + <script src="/resources/js/highlight.min.js"></script> + <script src="/resources/js/common.js"></script> <script>hljs.initHighlightingOnLoad();</script> </head> <body> @@ -827,7 +827,7 @@ <h5 id="configurable">Configurable</h5> <footer> <!--<div class="footer-top"> <h3>Make Your Papa Proud</h3> - <h4><a href="https://github.com/mholt/PapaParse">Star</a> and <a href="https://github.com/mholt/PapaParse/blob/gh-pagesresources/js/lovers.js">shout</a> if you love #PapaParse</h4> + <h4><a href="https://github.com/mholt/PapaParse">Star</a> and <a href="https://github.com/mholt/PapaParse/blob/gh-pages/resources/js/lovers.js">shout</a> if you love #PapaParse</h4> </div>--> <div class="footer-main"> <div class="grid-container"> From 89a3c88e253aa086bb655c9fbedfcc5bf5844637 Mon Sep 17 00:00:00 2001 From: Dennis Boldt <info@dennis-boldt.de> Date: Sat, 7 Jul 2018 00:20:07 +0200 Subject: [PATCH 5/5] Whitespace --- docs/docs.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs.html b/docs/docs.html index 93a3757b..39cf454d 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -271,7 +271,7 @@ <h5 id="unparse">Unparse</h5> <code>delimiter</code>: This property can be any valid delimiting character. </li> <li> - <code>newline</code>: This property can be any valid newline character(s). + <code>newline</code>: This property can be any valid newline character(s). </li> <li> <code>header</code>: Setting this property to <code>false</code> will omit the header row.