CSVTransform for Node.js

IssuesGitHub

Combining the stream and callback API

The steam and callback API are implemented inside the same module and they share the same implementation. It is possible to combine both. Records can be written with the stream writable API and obtained in the callback. They can also be provided as an argument and consumed with the stream readable API.

Using the stream writable API

In the input stream example, records are written with the write method and the resulting dataset is available in the user callback:

import { transform } from 'stream-transform';
import assert from 'assert';

// Create the parser
const transformer = transform(function(record){
  record.push(record.shift());
  return record;
}, function(err, output){
  assert.deepEqual(output, [
    [ '2', '3', '4', '1' ],
    [ 'b', 'c', 'd', 'a' ]
  ]);
});
// Write data to the stream
transformer.write(['1','2','3','4']);
transformer.write(['a','b','c','d']);
// Close the readable stream
transformer.end();

Using the stream readable API

In the input stream example, records are provided as an argument and consumed with the readable API:

import { transform } from 'stream-transform';
import assert from 'assert';

const records = [];
// Create the parser
const transformer = transform([
  ['1','2','3','4'],
  ['a','b','c','d']
], function(record){
  record.push(record.shift());
  return record;
})
// 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,
      [
        [ '2', '3', '4', '1' ],
        [ 'b', 'c', 'd', 'a' ]
      ]
    );
  });