Code coverage report for lib/streamer.js

Statements: 22.58% (7 / 31)      Branches: 0% (0 / 12)      Functions: 0% (0 / 5)      Lines: 22.58% (7 / 31)      Ignored: none     

All files » lib/ » streamer.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59    1 1   1           1       1                                                         1                         1  
'use strict';
 
var Writable = require('stream').Writable;
var Parser = require('./parser.js');
 
function Streamer(options) {
  Writable.call(this, options);
  this.buffer = '';
  this.sep = (options && options.separator) ? options.separator : undefined;
}
 
Streamer.prototype = Object.create(
  Writable.prototype, { constructor: { value: Streamer }});
 
 
Streamer.prototype._write = function (chunk, encoding, done) {
 
  this.buffer = this.buffer.concat(chunk.toString());
 
  if (this.sep === undefined) {
    // try to detect the separator if not provided
    this.sep = require('./csv.js').detect(this.buffer);
  }
 
  var csv = new Parser(this.buffer, this.sep);
  var rows = csv.File();
 
  if (csv.pointer < this.buffer.length) {
    this.buffer = this.buffer.slice(csv.pointer);
  }
  else {
    rows.pop();
    this.buffer = this.buffer.slice(csv.linePointer);
  }
  if (rows.length > 0) {
    var self = this;
    rows.forEach(function (row) {
        self.emit('data', row);
      }
    )
  }
  done();
}
 
Streamer.prototype.end = function () {
  var csv = new Parser(this.buffer, this.sep);
  var rows = csv.File();
  if (rows.length > 0) {
    var self = this;
    rows.forEach(function (row) {
        self.emit('data', row);
      }
    )
  }
  this.emit('end');
};
 
module.exports = Streamer;