|
|
|
@ -280,6 +280,9 @@
@@ -280,6 +280,9 @@
|
|
|
|
|
/** quote character */ |
|
|
|
|
var _quoteChar = '"'; |
|
|
|
|
|
|
|
|
|
/** whether to skip empty lines */ |
|
|
|
|
var _skipEmptyLines = false; |
|
|
|
|
|
|
|
|
|
unpackConfig(); |
|
|
|
|
|
|
|
|
|
var quoteCharRegex = new RegExp(_quoteChar, 'g'); |
|
|
|
@ -290,9 +293,9 @@
@@ -290,9 +293,9 @@
|
|
|
|
|
if (_input instanceof Array) |
|
|
|
|
{ |
|
|
|
|
if (!_input.length || _input[0] instanceof Array) |
|
|
|
|
return serialize(null, _input); |
|
|
|
|
return serialize(null, _input, _skipEmptyLines); |
|
|
|
|
else if (typeof _input[0] === 'object') |
|
|
|
|
return serialize(objectKeys(_input[0]), _input); |
|
|
|
|
return serialize(objectKeys(_input[0]), _input, _skipEmptyLines); |
|
|
|
|
} |
|
|
|
|
else if (typeof _input === 'object') |
|
|
|
|
{ |
|
|
|
@ -313,7 +316,7 @@
@@ -313,7 +316,7 @@
|
|
|
|
|
_input.data = [_input.data]; // handles input like [1,2,3] or ['asdf']
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return serialize(_input.fields || [], _input.data || []); |
|
|
|
|
return serialize(_input.fields || [], _input.data || [], _skipEmptyLines); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Default (any valid paths should return before this)
|
|
|
|
@ -335,6 +338,10 @@
@@ -335,6 +338,10 @@
|
|
|
|
|
|| _config.quotes instanceof Array) |
|
|
|
|
_quotes = _config.quotes; |
|
|
|
|
|
|
|
|
|
if (typeof _config.skipEmptyLines === 'boolean' |
|
|
|
|
|| typeof _config.skipEmptyLines === 'string') |
|
|
|
|
_skipEmptyLines = _config.skipEmptyLines; |
|
|
|
|
|
|
|
|
|
if (typeof _config.newline === 'string') |
|
|
|
|
_newline = _config.newline; |
|
|
|
|
|
|
|
|
@ -358,7 +365,7 @@
@@ -358,7 +365,7 @@
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** The double for loop that iterates the data and writes out a CSV string including header row */ |
|
|
|
|
function serialize(fields, data) |
|
|
|
|
function serialize(fields, data, skipEmptyLines) |
|
|
|
|
{ |
|
|
|
|
var csv = ''; |
|
|
|
|
|
|
|
|
@ -387,7 +394,10 @@
@@ -387,7 +394,10 @@
|
|
|
|
|
for (var row = 0; row < data.length; row++) |
|
|
|
|
{ |
|
|
|
|
var maxCol = hasHeader ? fields.length : data[row].length; |
|
|
|
|
var r = hasHeader ? fields : data[row]; |
|
|
|
|
|
|
|
|
|
if (skipEmptyLines !== 'greedy' || r.join('').trim() !== '') |
|
|
|
|
{ |
|
|
|
|
for (var col = 0; col < maxCol; col++) |
|
|
|
|
{ |
|
|
|
|
if (col > 0) |
|
|
|
@ -395,11 +405,10 @@
@@ -395,11 +405,10 @@
|
|
|
|
|
var colIdx = hasHeader && dataKeyedByField ? fields[col] : col; |
|
|
|
|
csv += safe(data[row][colIdx], col); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (row < data.length - 1) |
|
|
|
|
if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0)) |
|
|
|
|
csv += _newline; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return csv; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|