Files
people-randomizr/lib/csv-parser-client.ts

72 lines
1.6 KiB
TypeScript

export interface Person {
nom: string;
description: string;
type: string;
}
export function parseCSVFromText(content: string): Person[] {
const lines = content.split('\n');
// Skip header line
const dataLines = lines.slice(1);
const people: Person[] = [];
for (const line of dataLines) {
if (!line.trim()) continue;
// Parse CSV line (handling quoted fields)
const fields = parseCSVLine(line);
if (fields.length >= 7) {
const nom = fields[4]?.trim() || '';
const description = fields[5]?.trim() || '';
const type = fields[6]?.trim() || '';
// Skip empty names and service accounts
if (nom && !nom.startsWith('svc.') && !nom.startsWith('!') && nom !== 'datascience') {
people.push({
nom,
description,
type,
});
}
}
}
return people;
}
function parseCSVLine(line: string): string[] {
const fields: string[] = [];
let currentField = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
// Escaped quote
currentField += '"';
i++;
} else {
// Toggle quote state
inQuotes = !inQuotes;
}
} else if (char === ',' && !inQuotes) {
// Field separator
fields.push(currentField);
currentField = '';
} else {
currentField += char;
}
}
// Add last field
fields.push(currentField);
return fields.map(field => field.trim());
}