Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/fare-asso/fare-website. fare-asso.fr
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758export function parseCsv(input: string): string[][] { const rows: string[][] = [] let row: string[] = [] let field = "" let inQuotes = false let started = false
const pushField = (): void => { row.push(field) field = "" } const pushRow = (): void => { pushField() const isBlank = row.length === 1 && row[0] === "" // On saute les lignes vides if (!isBlank) rows.push(row) row = [] started = false }
for (let i = 0; i < input.length; i++) { const char = input[i] started = true
if (inQuotes) { if (char === '"') { if (input[i + 1] === '"') { field += '"' i++ } else { inQuotes = false } } else { field += char } continue }
if (char === '"') { inQuotes = true } else if (char === ",") { pushField() } else if (char === "\n") { pushRow() } else if (char === "\r") { if (input[i + 1] === "\n") i++ pushRow() } else { field += char } }
if (started || field !== "" || row.length > 0) { pushRow() }
return rows}