CSVParse for Node.js

IssuesGitHub

Combining a stream with a entire dataset

The main module exported by the package leverages the Node.js stream transform API. However, the input doesn't have to be a readable stream. Instead, it could be a CSV string and a Buffer. Also, the output doesn't have to be a writable stream, it could be a user callback function.

Uses it for convenience in case you are already interacting with a readable stream or a writable stream. It is not scalable because it implies that you either have all CSV dataset in memory and wish to pipe the generated records into a stream writer or that you have a stream reader generating a CSV data stream and wish to obtain a full dataset with all the records.

The signature of the output stream example is const stream = parse(input, [options]). It takes an input string and an options object as arguments and return a readable stream.

import assert from 'node:assert';
import { parse } from 'csv-parse';

const records = [];
parse(`
  "1","2","3"
  "a","b","c"
`, {
  trim: true,
  skip_empty_lines: true
})
// Use the readable stream api
  .on('readable', function(){
    let record; while ((record = this.read()) !== null) {
      records.push(record);
    }
  })
// When we are done, test that the parsed records matched what expected
  .on('end', function(){
    assert.deepStrictEqual(
      records,
      [
        [ '1','2','3' ],
        [ 'a','b','c' ]
      ]
    );
  });

Inversely, the signature of the input stream example is const stream = parse([options], callback). It takes an options object and a callback function as arguments and return a writable stream.

import assert from 'node:assert';
import { parse } from 'csv-parse';

// Create the parser
const parser = parse({
  delimiter: ':'
}, function(err, records){
  assert.deepStrictEqual(
    records,
    [
      [ 'root','x','0','0','root','/root','/bin/bash' ],
      [ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]
    ]
  );
});
// Write data to the stream
parser.write('root:x:0:0:root:/root:/bin/bash\n');
parser.write('someone:x:1022:1022::/home/someone:/bin/bash\n');
// Close the readable stream
parser.end();

About

The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.