Skip to content

Commit

Permalink
fix(data-table): format csv cell
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Dec 27, 2023
1 parent 6dce58f commit b22e52e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/data-table/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,21 @@ export function isColumnSorting (
)
}

function formatCsvCell (value: unknown): string {
if (typeof value === 'string') {
return value.replace(/,/g, '\\,')
} else if (value === null || value === undefined) {
return ''
} else {
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
return `${value}`.replace(/,/g, '\\,')
}
}

export function generateCsv (columns: TableColumn[], data: RowData[]): string {
const header = columns.map((col: any) => col.title).join(',')
const rows = data.map((row) => {
return columns.map((col: any) => row[col.key]).join(',')
return columns.map((col: any) => formatCsvCell(row[col.key])).join(',')
})
return [header, ...rows].join('\n')
}

0 comments on commit b22e52e

Please sign in to comment.