Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option quoteDataWithSpaces (default - true) to unparse config. #836

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/docs.html
Expand Up @@ -260,7 +260,8 @@ <h5 id="unparse-config-default">Default Unparse Config with all options</h5>
header: true,
newline: "\r\n",
skipEmptyLines: false, //other option is 'greedy', meaning skip delimiters, quotes, and whitespace.
columns: null //or array of strings
columns: null, //or array of strings
quoteDataWithSpaces: true
}
</code></pre>
</div>
Expand Down Expand Up @@ -337,6 +338,14 @@ <h5>Unparse Config Options</h5>
If <code>data</code> is an array of objects this option can be used to manually specify the keys (columns) you expect in the objects. If not set the keys of the first objects are used as column.
</td>
</tr>
<tr>
<td>
<code>quoteDataWithSpaces</code>
</td>
<td>
If <code>false</code>, field values that have spaces in the start or in the end will not be enclosed in quotes.
</td>
</tr>
<tr>
<td>
<code>escapeFormulae</code>
Expand Down
10 changes: 8 additions & 2 deletions papaparse.js
Expand Up @@ -285,6 +285,9 @@ License: MIT
/** whether to prevent outputting cells that can be parsed as formulae by spreadsheet software (Excel and LibreOffice) */
var _escapeFormulae = false;

/** whether to surround every datum that has spaces in the start or in the end with quotes */
var _quoteDataWithSpaces = true;

unpackConfig();

var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
Expand Down Expand Up @@ -367,6 +370,9 @@ License: MIT

if (typeof _config.escapeFormulae === 'boolean')
_escapeFormulae = _config.escapeFormulae;

if (typeof _config.quoteDataWithSpaces === 'boolean')
_quoteDataWithSpaces = _config.quoteDataWithSpaces;
}


Expand Down Expand Up @@ -464,8 +470,8 @@ License: MIT
|| (Array.isArray(_quotes) && _quotes[col])
|| hasAny(escapedQuoteStr, Papa.BAD_DELIMITERS)
|| escapedQuoteStr.indexOf(_delimiter) > -1
|| escapedQuoteStr.charAt(0) === ' '
|| escapedQuoteStr.charAt(escapedQuoteStr.length - 1) === ' ';
|| (_quoteDataWithSpaces && escapedQuoteStr.charAt(0) === ' ')
|| (_quoteDataWithSpaces && escapedQuoteStr.charAt(escapedQuoteStr.length - 1) === ' ');

return needsQuotes ? _quoteChar + escapedQuoteStr + _quoteChar : escapedQuoteStr;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/test-cases.js
Expand Up @@ -1881,6 +1881,13 @@ var UNPARSE_TESTS = [
config: { escapeFormulae: true, quotes: true, quoteChar: "'", escapeChar: "'" },
expected: '\'Col1\',\'Col2\',\'Col3\'\r\n\'\'\'=danger\',\'\'\'@danger\',\'safe\'\r\n\'safe=safe\',\'\'\'+danger\',\'\'\'-danger, danger\'\r\n\'\'\'+safe\',\'\'\'@safe\',\'safe, safe\''
},
{
description: "Use quoteDataWithSpaces set to false",
notes: "Papa should not add quotes to data with spaces in the start or in the end)",
input: { data: ["abc", "d", " ef "] },
config: { quoteDataWithSpaces: false },
expected: 'abc,d, ef '
},
];

describe('Unparse Tests', function() {
Expand Down