Init
This commit is contained in:
73
lib/csv-parser.ts
Normal file
73
lib/csv-parser.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
export interface Person {
|
||||
nom: string;
|
||||
description: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export async function parseCSV(filePath: string): Promise<Person[]> {
|
||||
const fs = await import('fs');
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user