refactor: move list sep logic to handlers

This commit is contained in:
riefive
2025-11-19 12:23:03 +07:00
parent 34d4b85e16
commit 05cc75fa95
4 changed files with 326 additions and 228 deletions
+7 -3
View File
@@ -16,6 +16,7 @@
*/
export function downloadCsv(
headers: string[] | null,
headerLabels: string[],
data: Array<Record<string, any> | any[]>,
filename = 'data.csv',
delimiter = ',',
@@ -66,7 +67,7 @@ export function downloadCsv(
return Object.values(row).map(escape).join(delimiter)
})
const headerRow = _headers ? _headers.join(delimiter) : null
const headerRow = headerLabels ? headerLabels.join(delimiter) : _headers ? _headers.join(delimiter) : null
const csvString = (addBOM ? '\uFEFF' : '') + [headerRow, ...rows].filter(Boolean).join('\r\n')
const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' })
@@ -95,6 +96,7 @@ export function downloadCsv(
*/
export async function downloadXls(
headers: string[] | null,
headerLabels: string[],
data: Array<Record<string, any> | any[]>,
filename = 'data.xlsx',
sheetName = 'Sheet1',
@@ -136,8 +138,10 @@ export async function downloadXls(
return Object.values(row)
})
// Combine headers and rows for sheet
const sheetData = _headers ? [_headers, ...rows] : rows
// Combine headers/labels and rows for sheet
// If caller provided headerLabels (as display labels), prefer them.
const sheetHeader = headerLabels ? headerLabels : _headers ? _headers : null
const sheetData = sheetHeader ? [sheetHeader, ...rows] : rows
// Create worksheet and workbook
const ws = utils.aoa_to_sheet(sheetData)