Skip to content

Commit d3eedbc

Browse files
authored
Merge pull request #702 from KxSystems/ee-csv
Always double quote CSV export
2 parents 9490704 + 6c924ec commit d3eedbc

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/utils/resultsRenderer.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,14 @@ export function formatResult(queryResult: string | number): string {
245245

246246
export function convertToCsv(data: any[]): string[] {
247247
const keys = Object.keys(data[0]);
248-
const header = keys.join(",");
249-
const rows = data.map((obj) => {
250-
return keys
251-
.map((key) => {
252-
return obj[key];
253-
})
254-
.join(",");
255-
});
248+
const header = keys.map((k) => normalize(k)).join(",");
249+
const rows = data.map((obj) =>
250+
keys.map((key) => normalize(obj[key])).join(","),
251+
);
256252
return [header, ...rows];
257253
}
254+
255+
function normalize(obj: any) {
256+
const o = `${obj}`;
257+
return `"${o.replace(/"/gs, '""')}"`;
258+
}

test/suite/webPanels/resultsPanelProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ describe("ResultsPanelProvider", () => {
375375
{ a: "2", b: "2" },
376376
{ a: "3", b: "3" },
377377
];
378-
const expectedOutput = ["a,b", "1,1", "2,2", "3,3"];
378+
const expectedOutput = ['"a","b"', '"1","1"', '"2","2"', '"3","3"'];
379379
const actualOutput = renderer.convertToCsv(inputQueryResult);
380380
assert.deepStrictEqual(actualOutput, expectedOutput);
381381
});

0 commit comments

Comments
 (0)