Browse Source

Add Option to skip lines with no content (#544)

pull/546/merge
Jay L 7 years ago committed by Sergi Almacellas Abellana
parent
commit
8e16bfa703
  1. 1
      docs/demo.html
  2. 2
      docs/docs.html
  3. 12
      papaparse.js
  4. 20
      tests/test-cases.js

1
docs/demo.html

@ -89,6 +89,7 @@ @@ -89,6 +89,7 @@
<input type="checkbox" id="skipEmptyLines"> Skip empty lines
<dfn>By default, empty lines are parsed; check to skip.</dfn>
</label>
</div>
<div class="grid-75 grid-parent">

2
docs/docs.html

@ -505,7 +505,7 @@ var csv = Papa.unparse({ @@ -505,7 +505,7 @@ var csv = Papa.unparse({
<code>skipEmptyLines</code>
</td>
<td>
If true, lines that are completely empty will be skipped. An empty line is defined to be one which evaluates to empty string.
If true, lines that are completely empty (those which evaluate to an empty string) will be skipped. If set to <code>'greedy'</code>, lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
</td>
</tr>
<tr>

12
papaparse.js

@ -1075,6 +1075,10 @@ @@ -1075,6 +1075,10 @@
_input = '';
};
function testEmptyLine(s) {
return _config.skipEmptyLines === 'greedy' ? s.join('').trim() === '' : s.length === 1 && s[0].length === 0;
}
function processResults()
{
if (_results && _delimiterError)
@ -1086,7 +1090,7 @@ @@ -1086,7 +1090,7 @@
if (_config.skipEmptyLines)
{
for (var i = 0; i < _results.data.length; i++)
if (_results.data[i].length === 1 && _results.data[i][0] === '')
if (testEmptyLine(_results.data[i]))
_results.data.splice(i--, 1);
}
@ -1215,7 +1219,8 @@ @@ -1215,7 +1219,8 @@
for (var j = 0; j < preview.data.length; j++)
{
if (skipEmptyLines && preview.data[j].length === 1 && preview.data[j][0].length === 0) {
if (skipEmptyLines && testEmptyLine(preview.data[j]))
{
emptyLinesCount++;
continue;
}
@ -1291,7 +1296,8 @@ @@ -1291,7 +1296,8 @@
}
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
function escapeRegExp(string) {
function escapeRegExp(string)
{
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

20
tests/test-cases.js

@ -1338,6 +1338,26 @@ var PARSE_TESTS = [ @@ -1338,6 +1338,26 @@ var PARSE_TESTS = [
truncated: false
}
}
},
{
description: "Parsing with skipEmptyLines set to 'greedy'",
notes: "Must parse correctly without lines with no content",
input: 'a,b\n\n,\nc,d\n , \n""," "\n , \n,,,,\n',
config: { skipEmptyLines: 'greedy' },
expected: {
data: [['a', 'b'], ['c', 'd']],
errors: []
}
},
{
description: "Parsing with skipEmptyLines set to 'greedy' with quotes and delimiters as content",
notes: "Must include lines with escaped delimiters and quotes",
input: 'a,b\n\n,\nc,d\n" , ",","\n""" """,""""""\n\n\n',
config: { skipEmptyLines: 'greedy' },
expected: {
data: [['a', 'b'], ['c', 'd'], [' , ', ','], ['" "', '""']],
errors: []
}
}
];

Loading…
Cancel
Save