Deviopath › TypeScript

CSV Parser with Quoted Fields

Hard · Parsing

Write parseCsv(text) that turns CSV text into an array of objects. The first row is the header with the keys.

- Fields are separated by , and rows by \n.

- A field that starts with " is quoted: it can contain commas and newlines, and "" inside it means one literal ". The surrounding quotes are not part of the value.

- Unquoted fields are kept exactly as written (no trimming).

- Completely empty lines are skipped (including a trailing newline at the end).

- If a row has fewer fields than the header, the missing values are "". Extra fields are ignored.

Return [] if there are no data rows.

Next: Parse Command-Line Arguments