Skip to content

Instantly share code, notes, and snippets.

@fmalk
Created August 7, 2026 00:05
Show Gist options
  • Select an option

  • Save fmalk/ebd385e7c9017822e02b29e3ecabec81 to your computer and use it in GitHub Desktop.

Select an option

Save fmalk/ebd385e7c9017822e02b29e3ecabec81 to your computer and use it in GitHub Desktop.
Minimal RFC4180-ish CSV parser
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