All files Colgroup.js

91.53% Statements 54/59
83.33% Branches 10/12
93.75% Functions 15/16
91.53% Lines 54/59
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168  1x 1x     1x             17x     1x 17x 17x   17x 37x 37x 37x 6x 4x   2x   31x 4x   27x                   1x 37x           1x 7x 7x 14x   7x                     1x 121x 1x   121x                   1x 35x                       1x 42x 42x 42x 42x 48x   42x                           1x 42x 42x 42x 42x             1x 1x 1x 1x 4x   1x             1x                                     1x 38x     1x 1x     4x        
// Import
var Column = require('./Column')
var range = require('lodash.range')
 
// Export
module.exports = Colgroup
 
/**
 * Store and manage column object of a Table
 * @param {Array} columns Optional array of Columns
 */
function Colgroup (columns) {
  this.columns = columns || []
}
 
Colgroup.prototype.parse = function (src) {
  this.columns = []
  var reColumn = /\|?\s*(.+?)\s*(\||\n)/g
  var result
  while ((result = reColumn.exec(src)) !== null) {
    var rule = result[1].trim()
    var size = result[1].length - 2
    if (/^:/.test(rule)) {
      if (/:$/.test(rule)) {
        this.push(new Column(Column.ALIGN_CENTER, size))
      } else {
        this.push(new Column(Column.ALIGN_LEFT, size))
      }
    } else if (/:$/.test(rule)) {
      this.push(new Column(Column.ALIGN_RIGHT, size))
    } else {
      this.push(new Column(Column.ALIGN_DEFAULT, size))
    }
  }
}
 
/**
 * Add a Column
 *
 * @param  {Column} column
 */
Colgroup.prototype.push = function (column) {
  this.columns.push(column)
}
 
/**
 * Generate a markdown formated colgroup
 */
Colgroup.prototype.toMarkdown = function () {
  var out = ['|']
  this.columns.forEach(function (column) {
    out.push(column.toMarkdown() + '|')
  })
  return out.join('')
}
 
/**
 * Get one column object at a given index
 *
 * This method always return a column object: if the index
 * is out of range, the colgroup is filled with empty column.
 *
 * @param {integer} atIndex
 */
Colgroup.prototype.getColumnAt = function (atIndex) {
  while (this.columns.length <= atIndex) {
    this.columns.push(new Column())
  }
  return this.columns[atIndex]
}
 
/**
 * Set how many chars a column could contain only if size is greater than
 * current size
 *
 * @param {integer} atIndex
 * @param {integer} size    How many char column atIndex must contain at least
 */
Colgroup.prototype.upSizeTo = function (atIndex, size) {
  this.getColumnAt(atIndex).upSizeTo(size)
}
 
/**
 * Get column size at a given index
 *
 * If a cell colspan is provided, return the cumulated columns  size
 *
 * @param {integer} atIndex
 * @param {integer} [colspan] Optional: how many columns must be cumulated to size
 * @return {integer} Column size or cumulated columns size
 */
Colgroup.prototype.getSizeAt = function (atIndex, colspan) {
  var self = this
  var size = 0
  colspan = colspan || 1
  range(atIndex, atIndex + colspan).forEach(function (index) {
    size += self.getColumnAt(index).getSize()
  })
  return size
}
 
/**
 * Get formated column size at a given index with padding
 *
 * The column size in term of "how many chars those colums can contain in a
 * formated markdown output":
 * A cell with a colspan > 1 must always contain the pipe for each column
 * collapsed but have a gain of two padding space for each column.
 *
 * @param {integer} atIndex
 * @param {integer} colspan Optional: how many columns must be cumulated to size
 */
Colgroup.prototype.getFormatedSizeAt = function (atIndex, colspan) {
  var size = this.getSizeAt(atIndex, colspan)
  colspan = colspan || 1
  size += (colspan - 1) * 2
  return size
}
 
/**
 * Return the table full size according to column size
 * @type {integer}
 */
Colgroup.prototype.getFullSize = function () {
  var self = this
  var size = 0
  self.columns.forEach(function (column) {
    size += column.getSize()
  })
  return size
}
 
/**
 * Return the table full formated size according to column size
 * @type {integer}
 */
Colgroup.prototype.getFullFormatedSize = function () {
  var size = this.getFullSize()
  var countCol = this.columns.length
 
  // Each column cost two padding and a pipe
  size += countCol * 3
 
  // A table cost one pipe
  size++
 
  return size
}
 
/**
 * Return column alignement
 *
 * @param {integer} atIndex
 * @return {String} A Column alignement constant
 */
Colgroup.prototype.getAlignAt = function (atIndex) {
  return this.getColumnAt(atIndex).align
}
 
Colgroup.prototype.getInfos = function () {
  return {
    size: this.getFullSize(),
    columns: this.columns.map(function (column) {
      return column.getInfos()
    })
  }
}