Parse a CSV File into Records
Parse a whole CSV document text into objects. Records are separated by \n or \r\n. Fields are separated by commas. A field may be wrapped in double quotes; inside quotes, commas and line breaks are literal and "" stands for one ". Quotes only open at the start of a field and a closing quote is always followed by a comma, a line break or the end of the text.
A line that is completely empty (no characters at all before its line break) is skipped. The first remaining record is the header. Every later record becomes an object mapping each header name to the field in the same position; missing fields become "" and extra fields are ignored.
Return the array of objects (empty if there is no data row).
Example 1
Input: text = "name,age\nAsha,30\n\"Lee, Bo\",41\n" Output: [{"name":"Asha","age":"30"},{"name":"Lee, Bo","age":"41"}] Explanation: A quoted field keeps its comma.
Example 2
Input: text = "id,note\r\n1,\"line1\nline2\"\r\n\r\n2,\"say \"\"hi\"\"\"" Output: [{"id":"1","note":"line1\nline2"},{"id":"2","note":"say \"hi\""}] Explanation: The quoted note spans two lines; the blank line is skipped.
Constraints
- 0 <= text.length <= 10^6
- Header names are unique
Hint 1
Write a character-by-character state machine with an "inside quotes" flag.
Hint 2
Track whether the current record has seen any character, so that "" on its own line is kept but an empty line is not.
Good practice for: Data Engineer, Backend Developer, Full Stack Developer, Data Scientist, Database Administrator, Automation Test Engineer
Run checks your code on 3 sample tests. Submit runs all 10 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.