Csv Parser is an example of string problems. In this post we will see how we can solve it in Javascript.

Problem Description

We're going to implement a simple CSV parsing function. There are two things to focus on. The first (and most importantly) is correctly parsing the CSV format. The second is writing clean code that another engineer would enjoy using. You may assume that the CSV file is correctly formatted.

csv_lines = [
 'John,Smith,[email protected],Los Angeles,10',
 'Jane,Roberts,[email protected],"San Francisco, CA",0',
 '"Alexandra ""Alex""",Menendez,[email protected],Miami,1',
 '1,2,,4,"5"'
]

An ideal parse will look like this:

[
 ['John', 'Smith', '[email protected]', 'Los Angeles', '1'],
 ['Jane', 'Roberts', '[email protected]', 'San Francisco, CA', '0'],
 ['Alexandra "Alex"', 'Menendez', '[email protected]', 'Miami', '1'],
 ['1','2','','4','5']
]

Solution: Please check the main.js snippet for the solution. If you have different approach in mind or have any suggestion for this implementation feel free to share in the comment below. Thanks!

Originally posted at: @github.com/jeantimex/javascript-problems-and-solutions