diff --git a/docs/docs.html b/docs/docs.html index e7911c91..36fc55c4 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -260,7 +260,8 @@
Default Unparse Config with all options
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 } @@ -337,6 +338,14 @@
Unparse Config Options
If data 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. + + + quoteDataWithSpaces + + + If false, field values that have spaces in the start or in the end will not be enclosed in quotes. + + escapeFormulae diff --git a/papaparse.js b/papaparse.js index e1825d14..ae290c51 100755 --- a/papaparse.js +++ b/papaparse.js @@ -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'); @@ -367,6 +370,9 @@ License: MIT if (typeof _config.escapeFormulae === 'boolean') _escapeFormulae = _config.escapeFormulae; + + if (typeof _config.quoteDataWithSpaces === 'boolean') + _quoteDataWithSpaces = _config.quoteDataWithSpaces; } @@ -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; } diff --git a/tests/test-cases.js b/tests/test-cases.js index 8d15b291..2a190dca 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -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() {