Browse Source

Added a fix where papaparse does not parse the data very well when there are some spaces between quoted fields and delimiter and/or newline. Issue - 452

Added tests to make sure the fix works.
pull/454/head
Smit Shah 7 years ago
parent
commit
697e6829d2
  1. 18
      papaparse.js
  2. 24
      tests/test-cases.js

18
papaparse.js

@ -1285,10 +1285,10 @@
continue; continue;
} }
var spacesBetweenQuoteAndDelimiter = extraSpacesLengthAfterQuote(nextDelim); var spacesBetweenQuoteAndDelimiter = extraSpaces(nextDelim);
// Closing quote followed by delimiter or 'unnecessary steps + delimiter' // Closing quote followed by delimiter or 'unnecessary steps + delimiter'
if (input[quoteSearch+1] === delim || spacesBetweenQuoteAndDelimiter > 0) 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 + spacesBetweenQuoteAndDelimiter + delimLen; cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen;
@ -1297,10 +1297,10 @@
break; break;
} }
var spacesBetweenQuoteAndNewLine = extraSpacesLengthAfterQuote(nextNewline); var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline);
// Closing quote followed by newline or 'unnecessary spaces + newLine' // Closing quote followed by newline or 'unnecessary spaces + newLine'
if (input.substr(quoteSearch+1, newlineLen) === newline || spacesBetweenQuoteAndNewLine > 0) 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 + spacesBetweenQuoteAndNewLine + newlineLen); saveRow(quoteSearch + 1 + spacesBetweenQuoteAndNewLine + newlineLen);
@ -1390,15 +1390,15 @@
} }
/** /**
* checks if there are extra spaces after closing quote and newLine without any text * checks if there are extra spaces after closing quote and given index without any text
* if Yes, returns the number of spaces * if Yes, returns the number of spaces
*/ */
function extraSpacesLengthAfterQuote(index) { function extraSpaces(index) {
var spaceLength = 0; var spaceLength = 0;
if (index !== -1) { if (index !== -1) {
var textBetweenQuoteAndNextNewLine = input.substring(quoteSearch + 1, index); var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index);
if (textBetweenQuoteAndNextNewLine && textBetweenQuoteAndNextNewLine.trim() == '') { if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() == '') {
spaceLength = textBetweenQuoteAndNextNewLine.length; spaceLength = textBetweenClosingQuoteAndIndex.length;
} }
} }
return spaceLength; return spaceLength;

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