-
Notifications
You must be signed in to change notification settings - Fork 5
[codex] add result export formats #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,20 @@ | ||
| import Papa from 'papaparse' | ||
| import type { ColumnMetadata } from '@/components/sql-editor/hooks/useEditorTabs' | ||
|
|
||
| export type ExportFormat = 'csv' | 'tsv' | 'json' | 'markdown' | ||
|
|
||
| export const EXPORT_FORMATS: Array<{ | ||
| value: ExportFormat | ||
| label: string | ||
| extension: string | ||
| mimeType: string | ||
| }> = [ | ||
| { value: 'csv', label: 'CSV', extension: 'csv', mimeType: 'text/csv;charset=utf-8;' }, | ||
| { value: 'tsv', label: 'TSV', extension: 'tsv', mimeType: 'text/tab-separated-values;charset=utf-8;' }, | ||
| { value: 'json', label: 'JSON', extension: 'json', mimeType: 'application/json;charset=utf-8;' }, | ||
| { value: 'markdown', label: 'Markdown', extension: 'md', mimeType: 'text/markdown;charset=utf-8;' }, | ||
| ] | ||
|
|
||
| function sanitizeCsvString(value: string): string { | ||
| return /^[\s]*[=+\-@\t\r]/.test(value) ? `'${value}` : value | ||
| } | ||
|
|
@@ -9,22 +23,107 @@ function sanitizeCsvCell(value: unknown): unknown { | |
| return typeof value === 'string' ? sanitizeCsvString(value) : value | ||
| } | ||
|
|
||
| export function exportToCsv( | ||
| function normalizeJsonValue(value: unknown): unknown { | ||
| if (value === undefined) return null | ||
| if (typeof value === 'bigint') return value.toString() | ||
| if (value instanceof Date) return value.toISOString() | ||
| return value | ||
| } | ||
|
|
||
| function serializeDelimited( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[], | ||
| filename: string | ||
| ) { | ||
| delimiter: ',' | '\t' | ||
| ): string { | ||
| const columnNames = columns.map((col) => col.name) | ||
| const csv = Papa.unparse({ | ||
| return Papa.unparse({ | ||
| fields: columnNames.map((name) => sanitizeCsvString(name)), | ||
| data: rows.map((row) => columnNames.map((name) => sanitizeCsvCell(row[name]))), | ||
| }, { | ||
| delimiter, | ||
| }) | ||
| } | ||
|
|
||
| function serializeJson( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[] | ||
| ): string { | ||
| const columnNames = columns.map((col) => col.name) | ||
| const projectedRows = rows.map((row) => Object.fromEntries( | ||
| columnNames.map((name) => [name, normalizeJsonValue(row[name])]) | ||
| )) | ||
| return `${JSON.stringify(projectedRows, null, 2)}\n` | ||
| } | ||
|
|
||
| function formatMarkdownCell(value: unknown): string { | ||
| if (value === null) return 'null' | ||
| if (value === undefined) return '' | ||
| return String(value) | ||
| .replace(/\|/g, '\\|') | ||
| .replace(/\r?\n/g, ' ') | ||
| } | ||
|
Comment on lines
+58
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+58
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| function serializeMarkdown( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[] | ||
| ): string { | ||
| const columnNames = columns.map((col) => col.name) | ||
| const lines = [ | ||
| `| ${columnNames.map(formatMarkdownCell).join(' | ')} |`, | ||
| `| ${columnNames.map(() => '---').join(' | ')} |`, | ||
| ] | ||
|
|
||
| for (const row of rows) { | ||
| lines.push(`| ${columnNames.map((name) => formatMarkdownCell(row[name])).join(' | ')} |`) | ||
| } | ||
|
|
||
| return `${lines.join('\n')}\n` | ||
| } | ||
|
|
||
| export function serializeExport( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[], | ||
| format: ExportFormat | ||
| ): string { | ||
| switch (format) { | ||
| case 'csv': | ||
| return serializeDelimited(columns, rows, ',') | ||
| case 'tsv': | ||
| return serializeDelimited(columns, rows, '\t') | ||
| case 'json': | ||
| return serializeJson(columns, rows) | ||
| case 'markdown': | ||
| return serializeMarkdown(columns, rows) | ||
| } | ||
| } | ||
|
|
||
| function downloadText(content: string, filename: string, mimeType: string) { | ||
| const blob = new Blob([content], { type: mimeType }) | ||
|
|
||
| const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }) | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = filename | ||
| link.click() | ||
| URL.revokeObjectURL(url) | ||
| } | ||
|
|
||
| export function exportRows( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[], | ||
| filename: string, | ||
| format: ExportFormat | ||
| ) { | ||
| const exportFormat = EXPORT_FORMATS.find((item) => item.value === format) | ||
| if (!exportFormat) return | ||
|
|
||
| downloadText(serializeExport(columns, rows, format), filename, exportFormat.mimeType) | ||
| } | ||
|
Comment on lines
+111
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| export function exportToCsv( | ||
| columns: Array<Pick<ColumnMetadata, 'name'>>, | ||
| rows: Record<string, unknown>[], | ||
| filename: string | ||
| ) { | ||
| exportRows(columns, rows, filename, 'csv') | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { serializeExport } from '../src/lib/export-csv' | ||
|
|
||
| const columns = [ | ||
| { name: 'id' }, | ||
| { name: 'name' }, | ||
| { name: 'note' }, | ||
| ] | ||
|
|
||
| const rows = [ | ||
| { id: 1, name: 'alpha', note: '=cmd' }, | ||
| { id: 2, name: 'pipe|name', note: 'line\nbreak' }, | ||
| ] | ||
|
|
||
| describe('serializeExport', () => { | ||
| it('serializes CSV and sanitizes formula-like cells', () => { | ||
| expect(serializeExport(columns, rows, 'csv')).toBe('id,name,note\r\n1,alpha,\'=cmd\r\n2,pipe|name,"line\nbreak"') | ||
| }) | ||
|
|
||
| it('serializes TSV', () => { | ||
| expect(serializeExport(columns, rows, 'tsv')).toBe('id\tname\tnote\r\n1\talpha\t\'=cmd\r\n2\tpipe|name\t"line\nbreak"') | ||
| }) | ||
|
|
||
| it('serializes JSON with the selected column order', () => { | ||
| expect(serializeExport(columns, [{ id: 1, name: undefined, note: 3n }], 'json')).toBe(`[ | ||
| { | ||
| "id": 1, | ||
| "name": null, | ||
| "note": "3" | ||
| } | ||
| ]\n`) | ||
| }) | ||
|
|
||
| it('serializes Markdown and escapes table pipes', () => { | ||
| expect(serializeExport(columns, rows, 'markdown')).toBe([ | ||
| '| id | name | note |', | ||
| '| --- | --- | --- |', | ||
| '| 1 | alpha | =cmd |', | ||
| '| 2 | pipe\\|name | line break |', | ||
| '', | ||
| ].join('\n')) | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
andbut leaves bareunchanged, which some Markdown renderers treat as a line break inside the table cell.