All files TBody.js

100% Statements 29/29
90% Branches 9/10
100% Functions 8/8
100% Lines 29/29
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  1x 1x     1x               25x     1x 25x 25x   25x 25x     62x     25x 62x 7x     7x   7x   55x 55x 55x       25x     1x 7x 7x 12x   7x     1x 14x     23x 4x   19x          
// Import
var MultilineRow = require('./MultilineRow')
var Row = require('./Row')
 
// Export
module.exports = TBody
 
/**
 * Table Body
 *
 * @param {Array} rows Optional array of Row objects
 */
function TBody (rows) {
  this.rows = rows || []
}
 
TBody.prototype.parse = function (src) {
  var self = this
  self.rows = []
 
  var stack = []
  var rows = []
 
  function isSplitter (line) {
    return /^[\s\-|]*$/.test(line)
  }
 
  src.trim().split('\n').forEach(function (line) {
    if (isSplitter(line)) {
      Iif (!stack.length) {
        // Ignore duplicate splitter ?
      } else {
        rows.push(new MultilineRow(stack))
      }
      stack = []
    } else {
      var row = new Row()
      row.parse(line)
      stack.push(row)
    }
  })
 
  self.rows = stack.length ? rows.concat(stack) : rows
}
 
TBody.prototype.toMarkdown = function (colgroup) {
  var out = []
  this.rows.forEach(function (row) {
    out.push(row.toMarkdown(colgroup))
  })
  return out.join('\n')
}
 
TBody.prototype.getInfos = function () {
  return {
    rows: this.rows.length,
    lines: this.rows.reduce(function (memo, row) {
      if (row instanceof MultilineRow) {
        return memo + row.rows.length
      } else {
        return memo + 1
      }
    }, 0)
  }
}