diff --git a/docs/docs.html b/docs/docs.html index c2d82bd..cd5c6eb 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -260,7 +260,8 @@ 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 } @@ -338,6 +339,14 @@ 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 057a16c..7a2f41b 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'); @@ -369,6 +372,9 @@ License: MIT if (typeof _config.escapeFormulae === 'boolean') _escapeFormulae = _config.escapeFormulae; + + if (typeof _config.quoteDataWithSpaces === 'boolean') + _quoteDataWithSpaces = _config.quoteDataWithSpaces; } @@ -455,8 +461,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 0c3a62d..0ffd5ef 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1906,6 +1906,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() {