Browse Source

Strip spaces from quoted values.

pull/521/head
Adi Roiban 7 years ago
parent
commit
528dad066a
  1. 9
      papaparse.js
  2. 22
      tests/test-cases.js

9
papaparse.js

@ -1393,10 +1393,19 @@
var nextNewline = input.indexOf(newline, cursor); var nextNewline = input.indexOf(newline, cursor);
var quoteCharRegex = new RegExp(escapeChar.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&') + quoteChar, 'g'); var quoteCharRegex = new RegExp(escapeChar.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&') + quoteChar, 'g');
var quoteSearch; var quoteSearch;
var trailingSpacesRegex = new RegExp('^[ ]+(".*)');
var trailingSpacesSearch;
// Parser loop // Parser loop
for (;;) 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 // Field has opening quote
if (input[cursor] === quoteChar) if (input[cursor] === quoteChar)
{ {

22
tests/test-cases.js

@ -167,10 +167,26 @@ var CORE_PARSER_TESTS = [
}, },
{ {
description: "Quoted field with whitespace around quotes", description: "Quoted field with whitespace around quotes",
input: 'A, "B" ,C', input: 'A, "B" ,"C" , "D",E "F',
notes: "The quotes must be immediately adjacent to the delimiter to indicate a quoted field", 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: { 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: [] errors: []
} }
}, },

Loading…
Cancel
Save