All files / app/lib fixed.js

91.17% Statements 93/102
76.92% Branches 20/26
100% Functions 4/4
91.17% Lines 93/102

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 10221x 21x 21x 6x 6x 6x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 3x 3x 1x 3x 3x 3x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 6x 21x 21x 21x 21x 21x 21x 19x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 27x 27x 27x 27x 27x 27x 27x 27x 9x 9x 9x 19x 9x 9x 9x 9x 9x 9x 9x 21x 21x 21x 7x 7x                   7x 7x 3x 3x 3x 7x 7x 7x 21x 21x 21x 3x 3x 21x 21x 21x
const Streamz = require('streamz');
const util = require('util');
 
function Fixed(layout,options) {
  if (!(this instanceof Fixed))
    return new Fixed(layout,options);
 
  Streamz.call(this);
 
  this.options = options || {};
 
  let n = 0;
 
  // If the layout is an array, we reduce to an object
  if(layout.length)
    layout = layout.reduce((p,d) => {
      p[d.field] = d;
      return p;
    },{});
 
  // Take note of the record length by looking for last `end`
  this.recordLength = Object.keys(layout).reduce((p,key) => {
    if (!isNaN(layout[key]))
      layout[key] = { length: layout[key] };
    
    const item = layout[key];
    if (!item.start)
      item.start = n;
    if (!item.end)
      item.end = item.start + item.length;
    n = item.end || 0;
    return Math.max(p, n);
  },0);
 
  this.layout = layout;
}
 
util.inherits(Fixed,Streamz);
 
Fixed.prototype.__line = 0;
 
Fixed.prototype._push = function() {
  
  if (!this.buffer || this.buffer.text.length < this.recordLength)
    return;
 
  const layout = this.layout;
 
  const obj = Object.create(this.buffer);
  obj.text = obj.text.slice(0,this.recordLength);
  
  Object.keys(layout)
    .forEach(key => {
      const e = layout[key];
      let val = obj.text.slice(e.start,e.end || e.start + e.length).trim();
      if (!val.length)
        return;   
      if (e.transform)
        val = e.transform(val, (this.__line + 1));
      if (val !== undefined)
        obj[key] = val;
    });
 
  if (this.options.clean)
    delete obj.text;
  else
    obj.__line = ++this.__line;
 
  this.push(obj);
 
  this.buffer.text = this.buffer.text.slice(this.recordLength);
  return this._push();
};
 
Fixed.prototype._fn = function(d) {
 
  if (d instanceof Buffer || typeof d !== 'object') {
    d = Object.create({},{
      // text should be non-enumerable
      text: {
        value: d.toString('utf8'),
        writable: true,
        configurable: true
      } 
    });
  }
 
  if (!this.buffer) {
    this.buffer = Object.create(!this.options.clean ? d : {});
    this.buffer.text = '';
  }
 
  this.buffer.text += d.text;
  this._push();
};
 
Fixed.prototype._flush = function(cb) {
  this._push();
  setImmediate( () => Streamz.prototype._flush(cb));
};
 
module.exports = Fixed;