Skip to content

Commit

Permalink
Fix: correctly escapes double quotes when converting ux.table to csv (#…
Browse files Browse the repository at this point in the history
…977) (#980)

* Fix: correctly escapes double quotes when converting ux.table to csv

modify replace with replaceAll as mentioned in this issue : #944 (comment)

* test: complete ux.table test to check if double quotes escaping is correct

Co-authored-by: Jules Bonnard <[email protected]>
  • Loading branch information
mdonnalley and julesbonnard authored Mar 1, 2024
1 parent 8cee1ce commit c9338b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/cli-ux/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Table<T extends Record<string, unknown>> {
const lineToBeEscaped = values.find(
(e: string) => e.includes('"') || e.includes('\n') || e.includes('\r\n') || e.includes('\r') || e.includes(','),
)
return values.map((e) => (lineToBeEscaped ? `"${e.replace('"', '""')}"` : e))
return values.map((e) => (lineToBeEscaped ? `"${e.replaceAll('"', '""')}"` : e))
}

private outputCSV() {
Expand Down
13 changes: 9 additions & 4 deletions test/cli-ux/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ describe('styled/table', () => {
id: '12"3',
name: 'supertable-test-2',
},
{
id: '1"2"3',
name: 'supertable-test-3',
},
{
id: '123',
name: 'supertable-test-3,comma',
name: 'supertable-test-4,comma',
},
{
id: '123',
name: 'supertable-test-4',
name: 'supertable-test-5',
},
],
columns,
Expand All @@ -173,8 +177,9 @@ describe('styled/table', () => {
expect(output.stdout).to.equal(`ID,Name
"123\n2","supertable-test-1"
"12""3","supertable-test-2"
"123","supertable-test-3,comma"
123,supertable-test-4\n`)
"1""2""3","supertable-test-3"
"123","supertable-test-4,comma"
123,supertable-test-5\n`)
})

fancy.stdout().end('outputs in csv without headers', (output) => {
Expand Down

0 comments on commit c9338b3

Please sign in to comment.