Parse CSV Rows with Typed Columns

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code

Write parseCsv(text, columns): the first non-empty line of text is a header of column names, and columns maps the names you want to a parser that turns a cell's text into a value (String, Number, or your own). Return one object per later non-empty line with just those columns, matched by header name. Cells are split on commas and trimmed, except that a cell in double quotes may hold commas, with two double quotes standing for one. Throw if a wanted column is missing from the header or a row's cell count differs from the header's. Type it so each row has each column's parsed type, parsers must take the cell's text, and other columns can't be read.

Your code must pass

  • Parses each column with its parser
  • Matches columns by header name and ignores the rest
  • Reads quoted cells with commas and quotes
  • Throws for a missing column or a short row
  • Has no type errors
  • Rows have each column's parsed type
  • Parsers must take the cell's text
  • Rejects reading a column that wasn't asked for