Browse Source

Add support for spaces between quotes and separators

Currently if there is an space between the quote char and the line (or
field) separator Paparse will throw and error of invalid format. This
commit removes the trailing spaces so the file is parsed correctly

Fixes #452.
pull/454/merge
Smit Shah 7 years ago committed by Sergi Almacellas Abellana
parent
commit
ec7653dae7
  1. 31
      papaparse.js
  2. 24
      tests/test-cases.js

31
papaparse.js

@ -1285,21 +1285,25 @@
continue; continue;
} }
// Closing quote followed by delimiter var spacesBetweenQuoteAndDelimiter = extraSpaces(nextDelim);
if (input[quoteSearch+1] === delim)
// Closing quote followed by delimiter or 'unnecessary steps + delimiter'
if (input[quoteSearch+1+spacesBetweenQuoteAndDelimiter] === delim)
{ {
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
cursor = quoteSearch + 1 + delimLen; cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
nextDelim = input.indexOf(delim, cursor); nextDelim = input.indexOf(delim, cursor);
nextNewline = input.indexOf(newline, cursor); nextNewline = input.indexOf(newline, cursor);
break; break;
} }
// Closing quote followed by newline var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
if (input.substr(quoteSearch+1, newlineLen) === newline)
// Closing quote followed by newline or 'unnecessary spaces + newLine'
if (input.substr(quoteSearch+1+spacesBetweenQuoteAndNewLine, newlineLen) === newline)
{ {
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
saveRow(quoteSearch + 1 + newlineLen); saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field
if (stepIsFunction) if (stepIsFunction)
@ -1385,6 +1389,21 @@
lastCursor = cursor; lastCursor = cursor;
} }
/**
* checks if there are extra spaces after closing quote and given index without any text
* if Yes, returns the number of spaces
*/
function extraSpaces(index) {
var spaceLength = 0;
if (index !== -1) {
var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() == '') {
spaceLength = textBetweenClosingQuoteAndIndex.length;
}
}
return spaceLength;
}
/** /**
* Appends the remaining input from cursor to the end into * Appends the remaining input from cursor to the end into
* row, saves the row, calls step, and returns the results. * row, saves the row, calls step, and returns the results.

24
tests/test-cases.js

@ -624,6 +624,30 @@ var PARSE_TESTS = [
errors: [] errors: []
} }
}, },
{
description: "Quoted fields with spaces between closing quote and next delimiter",
input: 'A,"B" ,C,D\r\nE,F,"G" ,H',
expected: {
data: [['A', 'B', 'C','D'],['E', 'F', 'G','H']],
errors: []
}
},
{
description: "Quoted fields with spaces between closing quote and next new line",
input: 'A,B,C,"D" \r\nE,F,G,"H" \r\nQ,W,E,R',
expected: {
data: [['A', 'B', 'C','D'],['E', 'F', 'G','H'],['Q', 'W', 'E','R']],
errors: []
}
},
{
description: "Quoted fields with spaces after closing quote",
input: 'A,"B" ,C,"D" \r\nE,F,"G" ,"H" \r\nQ,W,"E" ,R',
expected: {
data: [['A', 'B', 'C','D'],['E', 'F', 'G','H'],['Q', 'W', 'E','R']],
errors: []
}
},
{ {
description: "Mixed slash n and slash r should choose first as precident", description: "Mixed slash n and slash r should choose first as precident",
input: 'a,b,c\nd,e,f\rg,h,i\n', input: 'a,b,c\nd,e,f\rg,h,i\n',

Loading…
Cancel
Save