Skip to content

Commit

Permalink
Merge remote-tracking branch 'grafana/master' into min-max-field
Browse files Browse the repository at this point in the history
* grafana/master:
  CSV: escape quotes in toCSV  (grafana#16874)
  • Loading branch information
ryantxu committed May 3, 2019
2 parents 7874699 + 2af69cc commit aea5cd9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/grafana-ui/src/utils/csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ function norm(csv: string): string {

describe('write csv', () => {
it('should write the same CSV that we read', () => {
const firstRow = [10, 'this "has quotes" inside', true];
const path = __dirname + '/testdata/roundtrip.csv';
const csv = fs.readFileSync(path, 'utf8');
const data = readCSV(csv);
const out = toCSV(data, { headerStyle: CSVHeaderStyle.full });
expect(data.length).toBe(1);
expect(data[0].rows[0]).toEqual(firstRow);
expect(data[0].fields.length).toBe(3);
expect(norm(out)).toBe(norm(csv));

Expand All @@ -63,6 +65,7 @@ describe('write csv', () => {
const f = readCSV(shorter);
const fields = f[0].fields;
expect(fields.length).toBe(3);
expect(f[0].rows[0]).toEqual(firstRow);
expect(fields.map(f => f.name).join(',')).toEqual('a,b,c'); // the names
});
});
2 changes: 1 addition & 1 deletion packages/grafana-ui/src/utils/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function writeValue(value: any, config: CSVConfig): string {
const str = value.toString();
if (str.includes('"')) {
// Escape the double quote characters
return config.quoteChar + str.replace('"', '""') + config.quoteChar;
return config.quoteChar + str.replace(/"/gi, '""') + config.quoteChar;
}
if (str.includes('\n') || str.includes(config.delimiter)) {
return config.quoteChar + str + config.quoteChar;
Expand Down
2 changes: 1 addition & 1 deletion packages/grafana-ui/src/utils/testdata/roundtrip.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#name#a,b,c
#type#number,string,boolean
#unit#ms,,s
10,AA,true
10,"this ""has quotes"" inside",true
20,XX,false
30,YY,false
40,ZZ,true
Expand Down

0 comments on commit aea5cd9

Please sign in to comment.