From 66f1da203f2f267b3959c0748831e78d5e07a828 Mon Sep 17 00:00:00 2001 From: twotalesanimation <80506065+twotalesanimation@users.noreply.github.com> Date: Fri, 12 Jun 2026 17:18:26 +0200 Subject: [PATCH] csv take 4 --- lib/edl-utils.ts | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/edl-utils.ts b/lib/edl-utils.ts index 2ae67f1..7618af5 100644 --- a/lib/edl-utils.ts +++ b/lib/edl-utils.ts @@ -12,6 +12,35 @@ export interface EdlImportRow { existingShotId?: string; } +/** Parse a single CSV line, handling quoted fields and escaped double-quotes. */ +function parseCsvLine(line: string): string[] { + const result: string[] = []; + let current = ""; + let inQuotes = false; + for (let i = 0; i < line.length; i++) { + const ch = line[i]; + if (inQuotes) { + if (ch === '"') { + if (line[i + 1] === '"') { current += '"'; i++; } // escaped "" + else { inQuotes = false; } + } else { + current += ch; + } + } else { + if (ch === '"') { + inQuotes = true; + } else if (ch === ',') { + result.push(current.trim()); + current = ""; + } else { + current += ch; + } + } + } + result.push(current.trim()); + return result; +} + /** * Parse a VFX pull CSV into import rows. * Output Name format: {showId}_{episode}_{scene}_{shot}_BG01_TT_v001 @@ -22,9 +51,7 @@ export function parseEdlCsv(csvText: string): { rows: EdlImportRow[]; errors: st const lines = csvText.split(/\r?\n/).map((l) => l.trim()).filter(Boolean); if (lines.length < 2) return { rows: [], errors: ["CSV must have a header row and at least one data row"] }; - const unquote = (s: string) => s.replace(/^"|"$/g, "").trim(); - - const rawHeaders = lines[0].split(",").map(unquote).map((h) => h.toLowerCase()); + const rawHeaders = parseCsvLine(lines[0]).map((h) => h.toLowerCase()); const col = (name: string) => rawHeaders.indexOf(name); const outNameIdx = col("output name"); @@ -41,7 +68,7 @@ export function parseEdlCsv(csvText: string): { rows: EdlImportRow[]; errors: st const errors: string[] = []; for (let i = 1; i < lines.length; i++) { - const cells = lines[i].match(/("(?:[^"]|"")*"|[^,]*)/g)?.map(unquote) ?? []; + const cells = parseCsvLine(lines[i]); const get = (idx: number) => (idx !== -1 ? cells[idx] ?? "" : ""); const outputName = get(outNameIdx);