cisv
v0.4.8
Published
The csv parser on steroids.
Maintainers
Readme
CISV Node.js Binding
Native Node-API binding for the CISV C core.
Install
npm install cisvFrom source in this repository:
cd cisv
npm ci
npm run build
npm testQuick Start
const { cisvParser } = require('cisv');
const parser = new cisvParser({ delimiter: ',', trim: true });
const rows = parser.parseSync('data.csv');
console.log(rows.length);
console.log(rows[0]);Parser API
Constructor options
delimiter?: string(first character used)quote?: string(first character used)escape?: string | null(nullmeans RFC4180 doubled quote escaping)comment?: string | nulltrim?: booleanskipEmptyLines?: booleanrelaxed?: booleanskipLinesWithError?: booleanmaxRowSize?: numberfromLine?: numbertoLine?: number
Instance methods
parseSync(path: string): string[][]parse(path: string): Promise<string[][]>parseString(csv: string): string[][]write(chunk: Buffer | string): voidend(): voidgetRows(): string[][]clear(): voidsetConfig(config): thisgetConfig(): objecttransform(fieldIndex: number, kindOrFn: string | Function, context?): thistransformByName(fieldName: string, kindOrFn: string | Function, context?): thissetHeaderFields(fields: string[]): voidremoveTransform(fieldIndex: number): thisremoveTransformByName(fieldName: string): thisclearTransforms(): thisgetTransformInfo(): { cTransformCount: number, jsTransformCount: number, fieldIndices: number[] }getStats(): { rowCount: number, fieldCount: number, totalBytes: number, parseTime: number, currentLine: number }openIterator(path: string): thisfetchRow(): string[] | nullcloseIterator(): thisdestroy(): void
Static methods
cisvParser.countRows(path: string): numbercisvParser.countRowsWithConfig(path: string, config?): number
Transform Types
Built-in transform names:
uppercaselowercasetrimto_int(orint)to_float(orfloat)hash_sha256(orsha256)base64_encode(orbase64)
Examples
Async parse
const { cisvParser } = require('cisv');
(async () => {
const parser = new cisvParser();
const rows = await parser.parse('data.csv');
console.log(rows.length);
})();Streaming chunks
const fs = require('fs');
const { cisvParser } = require('cisv');
const parser = new cisvParser();
for (const chunk of [
Buffer.from('id,name\n1,'),
Buffer.from('john\n2,jane\n')
]) {
parser.write(chunk);
}
parser.end();
console.log(parser.getRows());Iterator mode (low memory)
const { cisvParser } = require('cisv');
const parser = new cisvParser({ delimiter: ',' });
parser.openIterator('large.csv');
let row;
while ((row = parser.fetchRow()) !== null) {
if (row[0] === 'stop') break;
}
parser.closeIterator();Name-based transforms
const { cisvParser } = require('cisv');
const parser = new cisvParser();
parser.setHeaderFields(['id', 'name', 'email']);
parser.transformByName('name', 'uppercase');
const rows = parser.parseString('id,name,email\n1,john,[email protected]');
console.log(rows[1][1]); // JOHNNotes
- Returned rows include the header row when the input has one.
removeTransform*currently removes JavaScript transforms; C-transform removal by index/name is not fully implemented yet.parse()runs in a worker thread for non-transform workloads; when transforms are attached it preserves current synchronous transform behavior for compatibility.
