/**
* Parses a CSV string into an array of objects.
* @param content The CSV string content.
* @returns An array of objects representing the CSV rows.
*/
export function parseCSV(content: string): any[] {
const lines = content.trim().split('\n');
if (lines.length < 2) return [];
const headers = parseCSVLine(lines[0]);
return lines.slice(1).map(line => {
const values = parseCSVLine(line);
const obj: any = {};
headers.forEach((header, index) => {
obj[header] = values[index] || '';
});
return obj;
});
}
/**
* Parses a single line of a CSV file, handling quoted fields.
* @param line The CSV line to parse.
* @returns An array of strings representing the fields in the line.
*/
function parseCSVLine(line: string): string[] {
const result: string[] = [];
let current = '';
let inQuotes = false;
let i = 0;
while (i < line.length) {
const char = line[i];
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"';
i += 2;
} else {
inQuotes = !inQuotes;
i++;
}
} else if (char === ',' && !inQuotes) {
result.push(current.trim());
current = '';
i++;
} else {
current += char;
i++;
}
}
result.push(current.trim());
return result;
}