From 82908947650ea0f6da93fe614f915b8cda13778e Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Wed, 23 Jul 2014 21:55:30 +0200 Subject: [PATCH] Add support for per-column quoting in Papa.unparse --- papaparse.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/papaparse.js b/papaparse.js index 735ff90..4eed95c 100644 --- a/papaparse.js +++ b/papaparse.js @@ -282,7 +282,8 @@ _delimiter = _config.delimiter; } - if (typeof _config.quotes === 'boolean') + if (typeof _config.quotes === 'boolean' + || _config.quotes instanceof Array) _quotes = _config.quotes; if (typeof _config.newline === 'string') @@ -337,7 +338,7 @@ if (col > 0) csv += _delimiter; var colIdx = hasHeader && dataKeyedByField ? fields[col] : col; - csv += safe(data[row][colIdx]); + csv += safe(data[row][colIdx], col); } if (row < data.length - 1) @@ -348,14 +349,15 @@ } // Encloses a value around quotes if needed (makes a value safe for CSV insertion) - function safe(str) + function safe(str, col) { if (typeof str === "undefined") return ""; str = str.toString().replace(/"/g, '""'); - var needsQuotes = _quotes + var needsQuotes = (typeof _quotes === 'boolean' && _quotes) + || (_quotes instanceof Array && _quotes[col]) || hasAny(str, global.Papa.BAD_DELIMITERS) || str.indexOf(_delimiter) > -1 || str.charAt(0) == ' '