From 8143db9f70d9bee3854a8f1d5aa729a885e78703 Mon Sep 17 00:00:00 2001 From: AkvelonAccount Date: Thu, 8 Oct 2020 11:10:43 +0300 Subject: [PATCH 1/4] Added option quoteDataWithSpaces (default - true) to unparse config. If quoteDataWithSpaces set to false, unparse result doesn't quote data with spaces in the start or in the end. --- papaparse.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/papaparse.js b/papaparse.js index e1825d1..ae290c5 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; } From ab42a04fe8a63389a1cd51cd90efcc272772ac56 Mon Sep 17 00:00:00 2001 From: AkvelonAccount Date: Thu, 8 Oct 2020 12:30:37 +0300 Subject: [PATCH 2/4] Added test to check working of quoteDataWithSpaces option --- tests/test-cases.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test-cases.js b/tests/test-cases.js index 8d15b29..2a190dc 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() { From 744d05be0c01426298ac6dfc3db50f28a84856db Mon Sep 17 00:00:00 2001 From: Alexander Gush Date: Mon, 12 Oct 2020 15:36:21 +0300 Subject: [PATCH 3/4] Modified docs.html --- docs/docs.html | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/docs.html b/docs/docs.html index e7911c9..d395c08 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, } @@ -337,6 +338,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 From 7c73643e257ddf52982ffd3ce2c690fbe6137bf3 Mon Sep 17 00:00:00 2001 From: Alexander Gush Date: Mon, 12 Oct 2020 15:38:16 +0300 Subject: [PATCH 4/4] Modified docs.html --- docs/docs.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs.html b/docs/docs.html index d395c08..36fc55c 100644 --- a/docs/docs.html +++ b/docs/docs.html @@ -260,8 +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, - quoteDataWithSpaces: true, + columns: null, //or array of strings + quoteDataWithSpaces: true }