Browse Source

Merge 89a3c88e25 into b5739c617f

pull/534/merge
Dennis Boldt 7 years ago committed by GitHub
parent
commit
1317432336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      docs/docs.html
  2. 26
      papaparse.js
  3. 12
      tests/test-cases.js

30
docs/docs.html

@ -247,13 +247,37 @@
<pre><code class="language-javascript">// defaults shown <pre><code class="language-javascript">// defaults shown
{ {
quotes: false, quotes: false,
onlyQuoteStrings: false,
quoteChar: '"', quoteChar: '"',
escapeChar: '"', escapeChar: '"',
delimiter: ",", delimiter: ",",
header: true, newline: "\r\n",
newline: "\r\n" header: true
}</code></pre> }</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> </li>
</ul> </ul>
</div> </div>

26
papaparse.js

@ -280,6 +280,9 @@
/** quote character */ /** quote character */
var _quoteChar = '"'; var _quoteChar = '"';
/** whether only strings should be quoted */
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
}
} }
@ -404,17 +412,23 @@
} }
/** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */ /** 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 ''; return '';
if (str.constructor === Date) if (value.constructor === Date)
return JSON.stringify(str).slice(1, 25); 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]) || (_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

@ -1605,6 +1605,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