All files / app/lib cut.js

100% Statements 52/52
88.23% Branches 15/17
100% Functions 4/4
100% Lines 52/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 5221x 21x 21x 2x 2x 2x 1x 2x 2x 1x 1x 1x 1x 2x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 9x 9x 4x 4x 4x 4x 4x 4x 4x 21x 21x 21x 4x 4x 4x 4x 4x 4x 4x 21x 21x 21x 1x 1x 21x 21x 21x
const Streamz = require('streamz');
const util = require('util');
 
function Cut(maxLen,options) {
  if (!(this instanceof Cut))
    return new Cut(maxLen,options);
 
  if(!maxLen && isNaN(maxLen))
    throw 'MaxLen not defined';
 
  Streamz.call(this,options);
 
  this.maxLen = +maxLen;
  this.options = options || {};
}
 
util.inherits(Cut,Streamz);
 
Cut.prototype.buffer = '';
 
Cut.prototype._proto = {};
 
Cut.prototype.line = 0;
 
Cut.prototype._push = function(end) {
  if (this.buffer.length < this.maxLen && !end)
    return;
 
  const obj = Object.create(this._proto);
  obj.text = this.buffer.slice(0,this.maxLen);
  obj.__line = this.line++;
  this.push(obj);
  this.buffer = this.buffer.slice(this.maxLen);
  return this._push();
};
 
Cut.prototype._fn = function(d) {
  if (d instanceof Buffer || typeof d !== 'object')
    d = d.toString('utf8');
 
  if (typeof d === 'object') this._proto = d;
  this.buffer += (typeof d == 'string') ? d : d.text;
 
  this._push();
};
 
Cut.prototype._flush = function(cb) {
  this._push();
  setImmediate( () => Streamz.prototype._flush(cb));
};
 
module.exports = Cut;