Created
August 7, 2026 00:05
-
-
Save fmalk/ebd385e7c9017822e02b29e3ecabec81 to your computer and use it in GitHub Desktop.
Minimal RFC4180-ish CSV parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs from 'fs'; | |
| // Minimal RFC4180-ish CSV parser: handles quoted fields with embedded commas. | |
| export function parseCsv(filePath: string): string[][] { | |
| const content = fs.readFileSync(filePath, 'utf-8').replace(/\r\n/g, '\n').trim(); | |
| const rows: string[][] = []; | |
| for (const line of content.split('\n')) { | |
| const fields: string[] = []; | |
| let field = ''; | |
| let inQuotes = false; | |
| for (let i = 0; i < line.length; i++) { | |
| const char = line[i]; | |
| if (inQuotes) { | |
| if (char === '"' && line[i + 1] === '"') { | |
| field += '"'; | |
| i++; | |
| } else if (char === '"') { | |
| inQuotes = false; | |
| } else { | |
| field += char; | |
| } | |
| } else if (char === '"') { | |
| inQuotes = true; | |
| } else if (char === ',') { | |
| fields.push(field); | |
| field = ''; | |
| } else { | |
| field += char; | |
| } | |
| } | |
| fields.push(field); | |
| rows.push(fields); | |
| } | |
| return rows; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment