All files Row.js

93.75% Statements 30/32
75% Branches 3/4
83.33% Functions 5/6
93.75% Lines 30/32
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  1x 1x 1x     1x           75x 75x     1x 75x   75x   75x 150x 150x 150x 150x 150x 150x               1x 150x                 1x 22x 22x 22x 22x 38x 38x 38x 38x 38x         22x           22x    
// Import
var repeat = require('lodash.repeat')
var range = require('lodash.range')
var Cell = require('./Cell')
 
// Export
module.exports = Row
 
/**
 * Table row
 */
function Row () {
  this.cells = []
  this.header = false
}
 
Row.prototype.parse = function (src) {
  this.cells = []
  // var re = /\|?(\s*([^\|]|\\\|)+?\s*)(([^\\]\|)+|\Z)/g
  var re = /\|?(\s*[^|]+\s*?)(\|+|)/g
  var result
  while ((result = re.exec(src)) !== null) {
    var content = result[1]
    var ending = result[2]
    var cell = new Cell(content)
    cell.colspan = ending.replace(/^\s*(\|{2,})\s*$/, '$1').length || 1
    cell.header = this.header
    this.push(cell)
  }
}
 
/**
 * Add a cell in row
 * @param  {Cell} cell
 */
Row.prototype.push = function (cell) {
  this.cells.push(cell)
}
 
/**
 * Generate a markdow representation of this Row
 * @param {Colgroup} colgroup
 *        A Colgroup object (provide column size and aligment informations)
 * @return {String}
 */
Row.prototype.toMarkdown = function (colgroup) {
  var out = []
  out.push('|')
  var index = 0
  this.cells.forEach(function (cell) {
    var align = colgroup.getAlignAt(index)
    var size = colgroup.getFormatedSizeAt(index, cell.colspan)
    out.push(cell.toMarkdown(align, size))
    out.push(repeat('|', cell.colspan))
    index += cell.colspan
  })
 
  // Add missing empty cells
  // TODO: Refactor: move this in the Table normalization process ?
  Iif (index < colgroup.columns.length) {
    range(index, colgroup.columns.length).forEach(function (index) {
      out.push(' ' + repeat(' ', colgroup.getFormatedSizeAt(index)) + ' |')
    })
  }
 
  return out.join('')
}