> text.split("\n")<p>I don't want to be negative but this will not work on CSV files that contain newlines inside columns. You should also implement escaping quotes - this is <i>very important</i> to ensure that the data can round-trip! Good for a first attempt, but as simple as CSV may look, there are subtle points that you should be aware of if you want to handle most if not all CSV out there. I've worked with lots of other implementations that get these little things wrong too, and it is particularly aggravating when CSV is supposed to be a pretty standard interchange format for tabular data. Please refer to RFC4180 for the details.
This kind of thing is incredibly useful.<p>Large frameworks are great, but generally I only adopt them when starting a new project; or the occasional large refactoring project.<p>Whereas targeted problem-solving libraries like this one get adopted all day long. Next time I need to parse a CSV on the client, you can bet the first thing I'll do is Google to see if there's a library I can download and be done with it.<p>So, great job solving a targeted problem! Keep building things and keep contributing. People will find code like this endlessly useful.
First anything I've contributed to the community.<p>As a 16 year-old just getting into software development, it'd be amazing to get feedback from hn.
If you are doing large-scale CSV parsing or encoding in Node, you may also find these two packages useful:<p><a href="https://www.npmjs.org/package/binary-csv" rel="nofollow">https://www.npmjs.org/package/binary-csv</a><p><a href="https://www.npmjs.org/package/csv-write-stream" rel="nofollow">https://www.npmjs.org/package/csv-write-stream</a><p>They are both written with handling very large files in mind, so they use buffers and streams for i/o.
The CSV 'format' is hell. I just wrote a blog highlighting some of the issues : <a href="http://tburette.github.io/blog/2014/05/25/so-you-want-to-write-your-own-CSV-code/" rel="nofollow">http://tburette.github.io/blog/2014/05/25/so-you-want-to-wri...</a>
Ever heard of jquery-csv?<p>I wrote the jquery-csv over two years ago with the goal of being the first completely RFC compliant CSV parser for Javascript.<p>It integrates with the CSV namespace but doesn't depend on it, uses pure vanilla JS, works with Node.js.<p>At the very least, if you want to claim RFC compliance you should have a test runner that verifies that your code doesn't break on the edge cases.<p><a href="https://code.google.com/p/jquery-csv/source/browse/test/test.html" rel="nofollow">https://code.google.com/p/jquery-csv/source/browse/test/test...</a><p>Once you have your state machine working, your best bet to optimize speed is by limiting string copy operations. I managed this by using a regex tokenizer that groups any non-terminals (ex data between quotes).<p>I wrote it with the intent of providing a lib that can effectively parse CSV data from the browser (loaded remotely via AJAX or locally via the HTML5 File API).<p>The biggest weakness of parsing CSV on the browser is the inability to process data streams. That 2GB memory limit on JS scrips in the browser becomes a fundamental weakness when you're trying to process large CSV files.<p>CSV in general is terrible for data storage, unless it's only used for serialization because arbitrarily reading any point in the input data stream requires the parser to start from the beginning. You're basically screwed if you can't hold the whole dataset in memory as a 2D array.
Consider starting at the default behavior with the documentation. For example, in the parse section, if no header option is provided, the first row will be returned as an array correct?<p>Start there: "The default behavior is to return each row of the CSV as an array." then "If the CSV file has a header, pass in {header: true} to get back an object using the header values as keys." then "If the file does not have a header, pass in ...".<p>Also, can a user override a header that is present in the data?
So will this let me make a csv file in JavaScript and actually then let the user download it as a file?<p>That would really simplify a lot of my workflows instead of having to make separate csv and HTML views I django.
in case someone wants a more mature parser: <a href="https://github.com/koles/ya-csv" rel="nofollow">https://github.com/koles/ya-csv</a>
Hey, I needed JS CSV parser few days ago. Here is my parser, I bet it is 100 times faster than yours :)<p>function parseCSV(str)
{
var obj = {};
var lines = str.split("\n");
var attrs = lines[0].split(",");
for(var i=0; i<attrs.length; i++) obj[attrs[i]] = [];
for(var i=1; i<lines.length-1; i++)
{
var line = lines[i].split(",");
for(var j=0; j<line.length; j++)
obj[attrs[j]].push(line[j]);
}
return obj;
}