All files index.js

100% Statements 26/26
100% Branches 6/6
100% Functions 6/6
100% Lines 26/26

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      1x 1x   1x   1x   1x   1x           4x 4x 4x         3x   3x 111x 111x 12x       3x       1x 1x         1x 3x 3x   3x 1x 1x     3x         8x     2x      
 
'use strict'
 
const fs = require('fs')
const stream = require('stream')
 
const parse = require('./parser')
 
const stringify = require('./stringifier')
 
module.exports = parse
 
module.exports.stringify = stringify
 
/* ------- Transform stream ------- */
 
class Parser extends stream.Transform {
  constructor (opts) {
    opts = opts || {}
    super({ objectMode: true })
    this._extract = parse.mkextract(opts)
  }
 
  _transform (data, encoding, done) {
    let block
    const lines = data.toString().split(/\n/)
 
    while (lines.length) {
      block = this._extract(lines.shift())
      if (block) {
        this.push(block)
      }
    }
 
    done()
  }
}
 
module.exports.stream = function stream (opts) {
  return new Parser(opts)
}
 
/* ------- File parser ------- */
 
module.exports.file = function file (file_path, done) {
  let opts = {}
  const collected = []
 
  if (arguments.length === 3) {
    opts = done
    done = arguments[2]
  }
 
  return fs.createReadStream(file_path, { encoding: 'utf8' })
    .on('error', done)
    .pipe(new Parser(opts))
    .on('error', done)
    .on('data', function (data) {
      collected.push(data)
    })
    .on('finish', function () {
      done(null, collected)
    })
}