The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { GENERIC_MIME_TYPES } from "./image.ts"; |
| 2 | |
| 3 | export function filePreviewDelimiter(file: { name: string; mimeType?: string }): "," | "\t" | null { |
| 4 | const mime = file.mimeType?.split(";", 1)[0]?.trim().toLowerCase(); |
| 5 | if (mime === "text/csv") return ","; |
| 6 | if (mime === "text/tab-separated-values") return "\t"; |
| 7 | if (mime && mime !== "text/plain" && !GENERIC_MIME_TYPES.has(mime)) return null; |
| 8 | if (/\.csv$/i.test(file.name)) return ","; |
| 9 | if (/\.tsv$/i.test(file.name)) return "\t"; |
| 10 | return null; |
| 11 | } |
| 12 | |
| 13 | export function parseDelimitedPreview( |
| 14 | text: string, |
| 15 | delimiter: "," | ";" | "\t", |
| 16 | limits: { maxRows: number; maxColumns: number; maxCellLength: number; detectCsv?: boolean } = { |
| 17 | maxRows: 100, |
| 18 | maxColumns: 30, |
| 19 | maxCellLength: 2000, |
| 20 | }, |
| 21 | ) { |
| 22 | if (delimiter === "," && limits.detectCsv !== false) { |
| 23 | delimiter = csvPreviewDelimiter(text); |
| 24 | } |
| 25 | const rows: string[][] = []; |
| 26 | let row: string[] = []; |
| 27 | let cell = ""; |
| 28 | let quoted = false; |
| 29 | let truncated = false; |
| 30 | let rowStart = text.charCodeAt(0) === 0xfeff ? 1 : 0; |
| 31 | const endCell = () => { |
| 32 | if (cell.length > limits.maxCellLength) { |
| 33 | let end = limits.maxCellLength; |
| 34 | const before = cell.charCodeAt(end - 1); |
| 35 | const after = cell.charCodeAt(end); |
| 36 | if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) end--; |
| 37 | cell = cell.slice(0, end); |
| 38 | truncated = true; |
| 39 | } |
| 40 | if (row.length < limits.maxColumns) row.push(cell); |
| 41 | else truncated = true; |
| 42 | cell = ""; |
| 43 | }; |
| 44 | for (let index = rowStart; index < text.length; index++) { |
| 45 | const char = text[index]; |
| 46 | if (char === '"') { |
| 47 | if (quoted && text[index + 1] === '"') { |
| 48 | if (cell.length <= limits.maxCellLength) cell += '"'; |
| 49 | else truncated = true; |
| 50 | index++; |
| 51 | continue; |
| 52 | } |
| 53 | if (quoted || cell === "") { |
| 54 | quoted = !quoted; |
| 55 | continue; |
| 56 | } |
| 57 | } |
| 58 | if (!quoted && (char === delimiter || char === "\n" || char === "\r")) { |
| 59 | endCell(); |
| 60 | if (char !== delimiter) { |
| 61 | rows.push(row); |
| 62 | row = []; |
| 63 | if (char === "\r" && text[index + 1] === "\n") index++; |
| 64 | rowStart = index + 1; |
| 65 | if (rows.length === limits.maxRows) |
| 66 | return { rows, truncated: truncated || index < text.length - 1 }; |
| 67 | } |
| 68 | } else if (cell.length <= limits.maxCellLength) cell += char; |
| 69 | else truncated = true; |
| 70 | } |
| 71 | if (rowStart < text.length) { |
| 72 | endCell(); |
| 73 | rows.push(row); |
| 74 | } |
| 75 | return { rows, truncated: truncated || quoted }; |
| 76 | } |
| 77 | |
| 78 | export function csvPreviewDelimiter(text: string) { |
| 79 | // Semicolons win only when they are consistent across the first two records and commas are not. |
| 80 | const commas = [0, 0]; |
| 81 | const semicolons = [0, 0]; |
| 82 | let record = 0; |
| 83 | let second = false; |
| 84 | let quoted = false; |
| 85 | let fieldStart = true; |
| 86 | for (let index = text.charCodeAt(0) === 0xfeff ? 1 : 0; index < text.length; index++) { |
| 87 | const char = text[index]; |
| 88 | if (quoted) { |
| 89 | if (char !== '"') continue; |
| 90 | if (text[index + 1] === '"') index++; |
| 91 | else quoted = false; |
| 92 | } else if (char === '"' && fieldStart) quoted = true; |
| 93 | else if (char === "\n" || char === "\r") { |
| 94 | if (record === 1) break; |
| 95 | if (char === "\r" && text[index + 1] === "\n") index++; |
| 96 | record = 1; |
| 97 | fieldStart = true; |
| 98 | continue; |
| 99 | } else if (char === ",") commas[record]!++; |
| 100 | else if (char === ";") semicolons[record]!++; |
| 101 | second ||= record === 1; |
| 102 | fieldStart = char === "," || char === ";"; |
| 103 | } |
| 104 | if (semicolons[0]! <= commas[0]!) return ","; |
| 105 | if (!second) return ";"; |
| 106 | const consistentCommas = commas[0]! > 0 && commas[1] === commas[0]; |
| 107 | return semicolons[1] === semicolons[0] && !consistentCommas ? ";" : ","; |
| 108 | } |