CSV.js
Simple, blazing-fast CSV parsing/encoding in JavaScript. Full RFC 4180 compliance.
Compatible with browsers (>IE8), AMD, and NodeJS.
Installation
Download csv.min.js
and reference to it using your preferred method.
If you use Bower, or npm, install the comma-separated-values
package.
Instantiation
Create a CSV instance with var csv = new CSV(data);
, where data
is a plain-text CSV string. You can supply options with the format var csv = new CSV(data, { option: value });
.
Options
cast
: true
to automatically cast numbers and booleans to their JavaScript equivalents. false
otherwise. Supply your own array
to override autocasting. Defaults to true
.lineDelimiter
: The string
that separates lines from one another. If parsing, defaults to autodetection. If encoding, defaults to '\r\n'
.cellDelimiter
: A 1-character-long string
that separates values from one another. If parsing, defaults to autodetection. If encoding, defaults to ','
.header
: true
if the first row of the CSV contains header values, or supply your own array
. Defaults to false
.
You can update an option's value any time after instantiation with csv.set(option, value)
.
Quickstart
For those accustomed to JavaScript, the CSV.js API:
var csv = new CSV(data, [options]);
var encoded = csv.encode();
var parsed = csv.parse();
csv.forEach(function(record) {
});
CSV.parse(data, options);
CSV.encode(data, options);
CSV.forEach(data, options, callback);
CSV.parse(data, { cast: ['String', 'Number', 'Number', 'Boolean'] });
CSV.encode(data, { cast: ['Primitive', 'Primitive', 'String'] });
Parsing
By default CSV.js will return an array of arrays
.
var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data).parse()
If the CSV's first row is a header, set header
to true
, and CSV.js will return an array of objects
.
var data = '\
year,age,status,sex,population\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, { header: true }).parse();
You may also supply your own header values, if the text does not contain them, by setting header
to an array
of field values.
var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, {
header: ['year', 'age', 'status', 'sex', 'population']
}).parse();
Encoding
CSV.js accepts an array of arrays
or an array of objects
.
var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]...];
new CSV(data).encode();
To add headers to an array of arrays
, set header
to an array
of header field values.
var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]];
new CSV(data, { header: ["year", "age", "status", "sex", "population"] }).encode();
To add headers to an array of objects
, just set header
to true
.
var data = [
{ year: 1850, age: 20, status: 0, sex: 1, population: 1017281 },
{ year: 1850, age: 20, status: 0, sex: 2, population: 1003841 }
];
new CSV(data, { header: true }).encode();
Streaming
If the dataset that you've provided is to be parsed, calling CSV.prototype.forEach
and supplying a function will call your function and supply it with the parsed record immediately after it's been parsed.
var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data).forEach(function(array) {
});
Likewise, if you've requested an array of objects
, you can still call CSV.prototype.forEach
:
var data = '\
year,age,status,sex,population\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, { header: true }).forEach(function(object) {
});
If you're dataset is to be encoded, CSV.prototype.forEach
will call your function and supply the CSV-encoded line immediately after the line has been encoded:
var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]];
new CSV(data).forEach(function(line) {
});
Casting
CSV.parse(data, { cast: ['String', 'Number', 'Number', 'Boolean'] });
CSV.encode(data, { cast: ['Primitive', 'Primitive', 'String'] });
Convenience Methods
CSV.parse(data, options)
CSV.encode(data, options)
CSV.forEach(data, options, callback)
Special Thanks