Browse Source

unparse uses skipEmptyRows properly for both true and greedy values

pull/554/head
jaymeans 7 years ago
parent
commit
cb32dab3c3
  1. 13
      papaparse.js
  2. 6
      tests/test-cases.js

13
papaparse.js

@ -293,9 +293,9 @@
if (_input instanceof Array) if (_input instanceof Array)
{ {
if (!_input.length || _input[0] instanceof Array) if (!_input.length || _input[0] instanceof Array)
return serialize(null, _input, _skipEmptyLines === 'greedy'); return serialize(null, _input, _skipEmptyLines);
else if (typeof _input[0] === 'object') else if (typeof _input[0] === 'object')
return serialize(objectKeys(_input[0]), _input, _skipEmptyLines === 'greedy'); return serialize(objectKeys(_input[0]), _input, _skipEmptyLines);
} }
else if (typeof _input === 'object') else if (typeof _input === 'object')
{ {
@ -316,7 +316,7 @@
_input.data = [_input.data]; // handles input like [1,2,3] or ['asdf'] _input.data = [_input.data]; // handles input like [1,2,3] or ['asdf']
} }
return serialize(_input.fields || [], _input.data || [], _skipEmptyLines === 'greedy'); return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
} }
// Default (any valid paths should return before this) // Default (any valid paths should return before this)
@ -365,7 +365,7 @@
} }
/** The double for loop that iterates the data and writes out a CSV string including header row */ /** The double for loop that iterates the data and writes out a CSV string including header row */
function serialize(fields, data, greedySkip) function serialize(fields, data, skipEmptyLines)
{ {
var csv = ''; var csv = '';
@ -394,8 +394,9 @@
for (var row = 0; row < data.length; row++) for (var row = 0; row < data.length; row++)
{ {
var maxCol = hasHeader ? fields.length : data[row].length; var maxCol = hasHeader ? fields.length : data[row].length;
var r = hasHeader ? fields : data[row];
if (!greedySkip || data[row].join('').trim() !== '') if (skipEmptyLines !== 'greedy' || r.join('').trim() !== '')
{ {
for (var col = 0; col < maxCol; col++) for (var col = 0; col < maxCol; col++)
{ {
@ -404,7 +405,7 @@
var colIdx = hasHeader && dataKeyedByField ? fields[col] : col; var colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
csv += safe(data[row][colIdx], col); csv += safe(data[row][colIdx], col);
} }
if (row < data.length - 1) if (row < data.length - 1 && (!skipEmptyLines || maxCol > 0))
csv += _newline; csv += _newline;
} }
} }

6
tests/test-cases.js

@ -1638,15 +1638,15 @@ var UNPARSE_TESTS = [
expected: 'date,not a date\r\n2018-05-04T21:08:03.269Z,16\r\n2018-05-08T15:20:22.000Z,32' expected: 'date,not a date\r\n2018-05-04T21:08:03.269Z,16\r\n2018-05-08T15:20:22.000Z,32'
}, },
{ {
description: "Returns empty rows when no content is passed and skipEmptyLines is false", description: "Returns empty rows when empty rows are passed and skipEmptyLines is false",
input: [[null, ' '], [], ['1', '2']], input: [[null, ' '], [], ['1', '2']],
config: {skipEmptyLines: false}, config: {skipEmptyLines: false},
expected: '," "\r\n\r\n1,2' expected: '," "\r\n\r\n1,2'
}, },
{ {
description: "Returns without empty rows and skipEmptyLines is true", description: "Returns without empty rows when skipEmptyLines is true",
input: [[null, ' '], [], ['1', '2']], input: [[null, ' '], [], ['1', '2']],
config: {skipEmptyLines: false}, config: {skipEmptyLines: true},
expected: '," "\r\n1,2' expected: '," "\r\n1,2'
}, },
{ {

Loading…
Cancel
Save