|
| 1 | +export function convertToCSV<T extends object>(data: T[]): string { |
| 2 | + if (!data || data.length === 0) { |
| 3 | + return ""; |
| 4 | + } |
| 5 | + |
| 6 | + const headers = Object.keys(data[0]) as Array<keyof T>; |
| 7 | + |
| 8 | + const csvRows = [ |
| 9 | + headers.join(","), |
| 10 | + ...data.map((row) => |
| 11 | + headers |
| 12 | + .map((fieldName) => { |
| 13 | + const value = row[fieldName]; |
| 14 | + let cell: string; |
| 15 | + |
| 16 | + if (value === null || value === undefined) { |
| 17 | + cell = ""; |
| 18 | + } else if (typeof value !== "string") { |
| 19 | + cell = String(value); |
| 20 | + } else { |
| 21 | + cell = value; |
| 22 | + } |
| 23 | + |
| 24 | + cell = cell.replace(/"/g, '""'); |
| 25 | + |
| 26 | + if (cell.search(/("|,|\n)/g) >= 0) { |
| 27 | + cell = `"${cell}"`; |
| 28 | + } |
| 29 | + |
| 30 | + return cell; |
| 31 | + }) |
| 32 | + .join(",") |
| 33 | + ), |
| 34 | + ]; |
| 35 | + |
| 36 | + return csvRows.join("\n"); |
| 37 | +} |
| 38 | + |
| 39 | +function getCurrentTimestamp(): string { |
| 40 | + const now = new Date(); |
| 41 | + |
| 42 | + const year = now.getFullYear().toString().padStart(4, "0"); |
| 43 | + const month = (now.getMonth() + 1).toString().padStart(2, "0"); |
| 44 | + const day = now.getDate().toString().padStart(2, "0"); |
| 45 | + |
| 46 | + const hours = now.getHours().toString().padStart(2, "0"); |
| 47 | + const minutes = now.getMinutes().toString().padStart(2, "0"); |
| 48 | + const seconds = now.getSeconds().toString().padStart(2, "0"); |
| 49 | + |
| 50 | + const milliseconds = now |
| 51 | + .getMilliseconds() |
| 52 | + .toString() |
| 53 | + .padStart(3, "0") |
| 54 | + .substring(0, 2); |
| 55 | + |
| 56 | + return `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`; |
| 57 | +} |
| 58 | + |
| 59 | +function appendTimestampToFileName( |
| 60 | + fileName: string, |
| 61 | + timestamp: string |
| 62 | +): string { |
| 63 | + const lastDotIndex = fileName.lastIndexOf("."); |
| 64 | + |
| 65 | + if (lastDotIndex === -1) { |
| 66 | + return `${fileName}${timestamp}`; |
| 67 | + } |
| 68 | + |
| 69 | + const namePart = fileName.substring(0, lastDotIndex); |
| 70 | + const extensionPart = fileName.substring(lastDotIndex); // Includes the dot |
| 71 | + |
| 72 | + return `${namePart}${timestamp}${extensionPart}`; |
| 73 | +} |
| 74 | + |
| 75 | +export function downloadCSV( |
| 76 | + csvContent: string, |
| 77 | + fileName: string = "data.csv" |
| 78 | +): void { |
| 79 | + const timestamp = getCurrentTimestamp(); |
| 80 | + const timestampedFileName = appendTimestampToFileName(fileName, timestamp); |
| 81 | + const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" }); |
| 82 | + const url = URL.createObjectURL(blob); |
| 83 | + const link = document.createElement("a"); |
| 84 | + link.href = url; |
| 85 | + link.setAttribute("download", timestampedFileName); |
| 86 | + |
| 87 | + document.body.appendChild(link); |
| 88 | + link.click(); |
| 89 | + |
| 90 | + document.body.removeChild(link); |
| 91 | + URL.revokeObjectURL(url); |
| 92 | +} |
| 93 | + |
| 94 | +export function exportToCSV<T extends object>( |
| 95 | + data: T[], |
| 96 | + fileName: string = "data.csv" |
| 97 | +) { |
| 98 | + const csv = convertToCSV(data); |
| 99 | + downloadCSV(csv, fileName); |
| 100 | +} |
0 commit comments