A javascript CSV parser.
There are many javascript CSV parsers but they either had dependencies or were difficult to use. This library aims to be straight forward to use and works similarly to the JSON parser.
Note This file does not handle the reading of files.
- Download csv.js
- Add it to your HTML file, or where ever you want to use it.
- ???
- Profit?
If you've ever parsed JSON in javascript then you basically know how to parse a CSV. Simply pass in a string containing CSV content to the parser.
// csv = "Header,...\nRows,...\n..."
var data = CSV.parse(csv);
// data = [['Header', ...], ['Rows', ...], ...]
To generate a CSV simply pass an array containing rows. The first row is optionally being the header.
// data = [['Header', ...], ['Rows', ...], ...]
csv = CSV.stringify(data);
// csv = "Header,...\nRows,...\n..."
The following options can be passed as the second argument to parse
or stringify
.
column separator. Defaults to comma.
row separator. Defaults to new line.
quote character. Defaults to double quote.
truncate fields that are larger. Defaults to null.
first row is the header row and should be omitted. When enabled the collection will feature the headers as a property on itself and columns can be referenced by the column name on the row. ie.
row.Name
. Defaults to false.
ignore blank rows when parsing.
all fields are quoted when generating.
- Fork the repo
- Make changes
- Submit pull request
- Test in Internet Explorer and Opera.
- Support generating from an array of objects.
- Benchmark