csv.js
csv.js copied to clipboard
String values with commas not encoded right
I like this library, but I'm having trouble with text values that may have commas:
var enc = csv.encode([{prop1: 'value, 1', prop2: 'value "2"', prop3: 'value, "3"'}])
// value of enc looks like:
// prop1,prop2,prop3
// "value, 1",value "2","value, "3""
var dec = csv.decode(enc);
// resulting object looks like so:
[{
prop1: "\"value, 1\",value \"2\",\"value, \"3\"\""
prop2: ""
prop3: ""
}]
In the above example, the third value containing both a comma and double-quotes is what has the most impact. If I omit the third property, it seems a little better (below), but I'm still getting an extra pair of double-quotes added around the decoded value if it has a comma within it:
var enc = csv.encode([{prop1: 'value, 1', prop2: 'value "2"'}])
// value of enc looks like:
// prop1,prop2
// "value, 1",value "2"
var dec = csv.decode(enc);
// resulting object looks like so:
[{
prop1: "\"value, 1\"",
prop2: "value \"2\""
}]
I would expect the first/last double-quotes to be excluded from in the resulting value for the first property (i.e., they are there to prevent the contained comma from being interpreted as a separator, but are not part of the actual value of the text). Is there something I'm not doing correctly?
I'm probably not going to get to look at this until the weekend, or next week. It's semi-important for a work project, but not highest priority atm.