35 lines
739 B
TypeScript
35 lines
739 B
TypeScript
/**
|
|
* CSV parser that handles quoted fields
|
|
*/
|
|
export function parseCSVLine(line: string): string[] {
|
|
const values: string[] = [];
|
|
let current = '';
|
|
let inQuotes = false;
|
|
|
|
for (let i = 0; i < line.length; i++) {
|
|
const char = line[i];
|
|
const nextChar = line[i + 1];
|
|
|
|
if (char === '"') {
|
|
if (inQuotes && nextChar === '"') {
|
|
// Escaped quote
|
|
current += '"';
|
|
i++;
|
|
} else {
|
|
// Toggle quote state
|
|
inQuotes = !inQuotes;
|
|
}
|
|
} else if (char === ',' && !inQuotes) {
|
|
// Field separator
|
|
values.push(current.trim());
|
|
current = '';
|
|
} else {
|
|
current += char;
|
|
}
|
|
}
|
|
|
|
// Add the last field
|
|
values.push(current.trim());
|
|
return values;
|
|
}
|