Skip to content

Commit

Permalink
Fix CSV parsing for strings with double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
texodus committed Oct 12, 2020
1 parent 8906d49 commit 2c9ede9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/perspective/src/js/view_formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const csvFormatter = Object.assign({}, jsonFormatter, {
switch (typeof x) {
case "object":
case "string":
return x.indexOf(delimiter) > -1 ? `"${x}"` : x.toString();
// CSV escapes with double double quotes, for real.
// [Section 2.7 of the fake CSV spec](https://tools.ietf.org/html/rfc4180)
return x.indexOf(delimiter) > -1 ? `"${x.replace(/\"/g, '""')}"` : x.toString();
case "number":
return x;
case "boolean":
Expand Down
14 changes: 14 additions & 0 deletions packages/perspective/test/js/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,20 @@ module.exports = perspective => {
table.delete();
});

it("Handles strings with quotation characters and commas", async function() {
let table = perspective.table({x: "string", y: "integer"});
table.update([
{x: "Test, hello!", y: 1},
{x: 'Test2"', y: 2},
{x: 'Test3, Hello!"', y: 3}
]);
let view = table.view();
let result = await view.to_csv();
expect(result).toEqual(`x,y\r\n"Test, hello!",1\r\nTest2",2\r\n"Test3, Hello!""",3`);
view.delete();
table.delete();
});

it("Transitively loads a CSV created from `to_csv()` on a table with a datetime column", async function() {
// Assert that the CSV parser can handle POSIX timestamps.
let table = perspective.table(arrow_date_data);
Expand Down

0 comments on commit 2c9ede9

Please sign in to comment.