From 528dad066a1639c652efbc149f10f3673124e061 Mon Sep 17 00:00:00 2001 From: Adi Roiban Date: Sat, 9 Jun 2018 08:42:25 +0100 Subject: [PATCH] Strip spaces from quoted values. --- papaparse.js | 9 +++++++++ tests/test-cases.js | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/papaparse.js b/papaparse.js index 8106e8d..e414810 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1393,10 +1393,19 @@ var nextNewline = input.indexOf(newline, cursor); var quoteCharRegex = new RegExp(escapeChar.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&') + quoteChar, 'g'); var quoteSearch; + var trailingSpacesRegex = new RegExp('^[ ]+(".*)'); + var trailingSpacesSearch; // Parser loop for (;;) { + trailingSpacesSearch = input.substring(cursor).match(trailingSpacesRegex); + if (trailingSpacesSearch) { + // We have a quoted value with trailing space(s), so we + // ignore the spaces. + cursor += 1; + continue; + } // Field has opening quote if (input[cursor] === quoteChar) { diff --git a/tests/test-cases.js b/tests/test-cases.js index b7f55ef..2969cd7 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -167,10 +167,26 @@ var CORE_PARSER_TESTS = [ }, { description: "Quoted field with whitespace around quotes", - input: 'A, "B" ,C', - notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field", + input: 'A, "B" ,"C" , "D",E "F', + notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field, but there are leading or trailing spaces they are ignored", expected: { - data: [['A', ' "B" ', 'C']], + data: [['A', 'B', 'C', 'D', 'E "F']], + errors: [] + } + }, + { + description: "Quoted fields with comma", + input: '"A","b,C"\nD"E,f', + expected: { + data: [['A', 'b,C'],['D"E', 'f']], + errors: [] + } + }, + { + description: "Quoted fields with comma and whitespace before and after quotes", + input: '"A" , "b , C" \n D"E , f ', + expected: { + data: [['A', 'b , C'],[' D"E ', ' f ']], errors: [] } },