|
| 1 | +// Data formatting functions for different export formats |
| 2 | + |
| 3 | +import type { ExportFormat } from './types'; |
| 4 | + |
| 5 | +export function convertToCSV<T extends Record<string, unknown>>(data: T[]): string { |
| 6 | + if (data.length === 0) return ''; |
| 7 | + |
| 8 | + const headers = Object.keys(data[0] || {}).join(','); |
| 9 | + const rows = data |
| 10 | + .map((row) => |
| 11 | + Object.values(row) |
| 12 | + .map((value) => { |
| 13 | + if (value === null || value === undefined) { |
| 14 | + return ''; |
| 15 | + } |
| 16 | + const stringValue = String(value); |
| 17 | + // Escape commas, quotes, and newlines in CSV |
| 18 | + if ( |
| 19 | + stringValue.includes(',') || |
| 20 | + stringValue.includes('"') || |
| 21 | + stringValue.includes('\n') |
| 22 | + ) { |
| 23 | + return `"${stringValue.replace(/"/g, '""')}"`; |
| 24 | + } |
| 25 | + return stringValue; |
| 26 | + }) |
| 27 | + .join(',') |
| 28 | + ) |
| 29 | + .join('\n'); |
| 30 | + |
| 31 | + return `${headers}\n${rows}`; |
| 32 | +} |
| 33 | + |
| 34 | +export function convertToTXT<T extends Record<string, unknown>>(data: T[]): string { |
| 35 | + if (data.length === 0) return ''; |
| 36 | + |
| 37 | + const headers = Object.keys(data[0] || {}).join('\t'); |
| 38 | + const rows = data |
| 39 | + .map((row) => |
| 40 | + Object.values(row) |
| 41 | + .map((value) => { |
| 42 | + if (value === null || value === undefined) { |
| 43 | + return ''; |
| 44 | + } |
| 45 | + // Replace tabs and newlines to maintain format |
| 46 | + return String(value).replace(/[\t\n\r]/g, ' '); |
| 47 | + }) |
| 48 | + .join('\t') |
| 49 | + ) |
| 50 | + .join('\n'); |
| 51 | + |
| 52 | + return `${headers}\n${rows}`; |
| 53 | +} |
| 54 | + |
| 55 | +export function convertToProto<T extends Record<string, unknown>>( |
| 56 | + data: T[], |
| 57 | + typeName: string |
| 58 | +): string { |
| 59 | + if (data.length === 0) return ''; |
| 60 | + |
| 61 | + let protoContent = `# Protocol Buffer Text Format\n# Type: ${typeName}\n\n`; |
| 62 | + |
| 63 | + for (const [index, row] of data.entries()) { |
| 64 | + protoContent += `${typeName} {\n`; |
| 65 | + |
| 66 | + for (const [key, value] of Object.entries(row)) { |
| 67 | + if (value !== null && value !== undefined) { |
| 68 | + const fieldName = key.toLowerCase().replace(/[^a-z0-9_]/g, '_'); |
| 69 | + |
| 70 | + if (typeof value === 'string') { |
| 71 | + // Escape quotes in string values |
| 72 | + const escapedValue = value.replace(/"/g, '\\"').replace(/\n/g, '\\n'); |
| 73 | + protoContent += ` ${fieldName}: "${escapedValue}"\n`; |
| 74 | + } else if (typeof value === 'number') { |
| 75 | + protoContent += ` ${fieldName}: ${value}\n`; |
| 76 | + } else if (typeof value === 'boolean') { |
| 77 | + protoContent += ` ${fieldName}: ${value}\n`; |
| 78 | + } else { |
| 79 | + // Convert other types to string |
| 80 | + const stringValue = String(value).replace(/"/g, '\\"').replace(/\n/g, '\\n'); |
| 81 | + protoContent += ` ${fieldName}: "${stringValue}"\n`; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + protoContent += '}\n'; |
| 87 | + if (index < data.length - 1) { |
| 88 | + protoContent += '\n'; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return protoContent; |
| 93 | +} |
| 94 | + |
| 95 | +export function formatData<T extends Record<string, unknown>>( |
| 96 | + data: T[], |
| 97 | + format: ExportFormat, |
| 98 | + typeName: string |
| 99 | +): string { |
| 100 | + switch (format) { |
| 101 | + case 'csv': |
| 102 | + return convertToCSV(data); |
| 103 | + case 'txt': |
| 104 | + return convertToTXT(data); |
| 105 | + case 'proto': |
| 106 | + return convertToProto(data, typeName); |
| 107 | + case 'json': |
| 108 | + default: |
| 109 | + return JSON.stringify(data, null, 2); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export function getFileExtension(format: ExportFormat): string { |
| 114 | + switch (format) { |
| 115 | + case 'csv': |
| 116 | + return 'csv'; |
| 117 | + case 'txt': |
| 118 | + return 'txt'; |
| 119 | + case 'proto': |
| 120 | + return 'proto.txt'; |
| 121 | + case 'json': |
| 122 | + default: |
| 123 | + return 'json'; |
| 124 | + } |
| 125 | +} |
0 commit comments