All files Column.js

96.55% Statements 28/29
91.67% Branches 11/12
83.33% Functions 5/6
96.55% Lines 28/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 67 68 69 70 71 72 73 74 75 76 77 78  1x     1x                 38x 38x     1x 1x 1x 1x         1x 14x 14x 14x   1x 1x   1x 1x   1x 1x   11x   14x               1x                   1x 35x 35x           1x 66x     1x 4x          
// Import
var repeat = require('lodash.repeat')
 
// Export
module.exports = Column
 
/**
 * A table column description
 *
 * @param {String|null} align Alignement (see class constants)
 * @param {Integer} size  Column width (char count)
 */
function Column (align, size) {
  this.align = align
  this.size = size
}
 
Column.ALIGN_LEFT = 'left'
Column.ALIGN_RIGHT = 'right'
Column.ALIGN_CENTER = 'center'
Column.ALIGN_DEFAULT = 'default'
 
/**
 * Generate a markdown formated column
 */
Column.prototype.toMarkdown = function () {
  var out = []
  var size = this.getSize()
  switch (this.align) {
    case 'left':
      out.push(':' + repeat('-', size + 1))
      break
    case 'right':
      out.push(repeat('-', size + 1) + ':')
      break
    case 'center':
      out.push(':' + repeat('-', size) + ':')
      break
    default:
      out.push(repeat('-', size + 2))
  }
  return out.join('')
}
 
/**
 * Set how many chars this column could contain
 *
 * @param {integer} size
 */
Column.prototype.setSize = function (size) {
  this.size = size
}
 
/**
 * Set how many chars this column could contain only if size is greater than
 * current size
 *
 * @param {integer} size
 */
Column.prototype.upSizeTo = function (size) {
  this.size = this.size || 1
  this.size = size > this.size ? size : this.size
}
 
/**
 * Get the column size (always >= 1)
 */
Column.prototype.getSize = function () {
  return (!this.size || (this.size < 1)) ? 1 : this.size
}
 
Column.prototype.getInfos = function () {
  return {
    size: this.size,
    align: this.align
  }
}