Browse Source

Use 'greedy' skipEmptyLines in unparse (#554)

Resolves #553
pull/575/head
Jay L 7 years ago committed by Sergi Almacellas Abellana
parent
commit
953dd9873c
  1. 23
      papaparse.js
  2. 18
      tests/test-cases.js

23
papaparse.js

@ -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;
}

18
tests/test-cases.js

@ -1636,6 +1636,24 @@ var UNPARSE_TESTS = [ @@ -1636,6 +1636,24 @@ var UNPARSE_TESTS = [
description: "Date objects are exported in its ISO representation",
input: [{date: new Date("2018-05-04T21:08:03.269Z"), "not a date": 16}, {date: new Date("Tue May 08 2018 08:20:22 GMT-0700 (PDT)"), "not a date": 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 empty rows are passed and skipEmptyLines is false",
input: [[null, ' '], [], ['1', '2']],
config: {skipEmptyLines: false},
expected: '," "\r\n\r\n1,2'
},
{
description: "Returns without empty rows when skipEmptyLines is true",
input: [[null, ' '], [], ['1', '2']],
config: {skipEmptyLines: true},
expected: '," "\r\n1,2'
},
{
description: "Returns without rows with no content when skipEmptyLines is 'greedy'",
input: [[null, ' '], [], ['1', '2']],
config: {skipEmptyLines: 'greedy'},
expected: '1,2'
}
];

Loading…
Cancel
Save