From 697e6829d2956f11751f31831752822c9e425361 Mon Sep 17 00:00:00 2001 From: Smit Shah Date: Tue, 6 Feb 2018 12:08:45 -0800 Subject: [PATCH] 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. --- papaparse.js | 18 +++++++++--------- tests/test-cases.js | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/papaparse.js b/papaparse.js index 1410942..c1f1ec4 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1285,10 +1285,10 @@ continue; } - var spacesBetweenQuoteAndDelimiter = extraSpacesLengthAfterQuote(nextDelim); + var spacesBetweenQuoteAndDelimiter = extraSpaces(nextDelim); // 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)); cursor = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter + delimLen; @@ -1297,10 +1297,10 @@ break; } - var spacesBetweenQuoteAndNewLine = extraSpacesLengthAfterQuote(nextNewline); + var spacesBetweenQuoteAndNewLine = extraSpaces(nextNewline); // 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)); 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 */ - function extraSpacesLengthAfterQuote(index) { + function extraSpaces(index) { var spaceLength = 0; if (index !== -1) { - var textBetweenQuoteAndNextNewLine = input.substring(quoteSearch + 1, index); - if (textBetweenQuoteAndNextNewLine && textBetweenQuoteAndNextNewLine.trim() == '') { - spaceLength = textBetweenQuoteAndNextNewLine.length; + var textBetweenClosingQuoteAndIndex = input.substring(quoteSearch + 1, index); + if (textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex.trim() == '') { + spaceLength = textBetweenClosingQuoteAndIndex.length; } } return spaceLength; diff --git a/tests/test-cases.js b/tests/test-cases.js index 5352dae..503e905 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -624,6 +624,30 @@ var PARSE_TESTS = [ 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", input: 'a,b,c\nd,e,f\rg,h,i\n',