Incremental CSV Parser
A fast & lightweight CSV parser with an incremental parsing api. No
dependencies; browser compatible. Includes ESM and CJS builds. All column
values are strings (numbers and dates will not automatically be parsed).
Fully compatible with rfc4180.
Basic parsing
import { parse } from 'incremental-csv-parser';
const results = await parse(`a,b,c,d
1,2,3,4
5,6,7,8
`);
console.log(results);
Incremental parsing
import { CSVParser } from 'incremental-csv-parser';
const results = [];
const parser = new CSVParser((row) => {
results.push(row);
});
parser.process('a,b,c,d\n');
parser.process('1,2,3,4\n');
console.log(results.shift());
parser.process('5,6,7,8');
parser.flush();
console.log(results.shift());
Typescript support
If column names are known ahead of time, they can be passed in via generics.
import { CSVParser } from 'incremental-csv-parser';
type ColumnName = 'a' | 'b' | 'c' | 'd';
const results: Record<ColumnName, string> = [];
const parser = new CSVParser<ColumName>((row) => {
results.push(row);
});
Both the parse
function and CSVParser
class have an optional second argument
for explicitly providing column names for a csv.
import { CSVParser } from 'incremental-csv-parser';
const columns = ['a', 'b', 'c', 'd'];
const results = parse(`1,2,3,4
5,6,7,8`, columns);
console.log(results);