All files Cell.js

95.65% Statements 22/23
81.82% Branches 9/11
75% Functions 3/4
95.65% Lines 22/23
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     1x                       150x 150x 150x 150x           1x                   1x 38x                   1x 38x 38x   38x     2x 2x   2x 2x     34x   38x    
// Import
var Column = require('./Column')
var pad = require('lodash.pad')
var padleft = require('lodash.padleft')
var padright = require('lodash.padright')
 
// Export
module.exports = Cell
 
/**
 * A table cell
 *
 * @param {String} content Cell content
 * @param {Object} options
 *        Cell option:
 *          - colspan {integer} how many collapsed columns (default: 1)
 *          - header  {boolean} is a header cell (default: false)
 */
function Cell (content, options) {
  options = options || {}
  this.content = content
  this.colspan = options.colspan || 1
  this.header = options.header === true
}
 
/**
 * @return {Cell} A clone of this Cell
 */
Cell.prototype.clone = function () {
  return new Cell(this.content, {
    colspan: this.colspan,
    header: this.header
  })
}
 
/**
 * @return {integer} Cell content length (without whitespace from both ends)
 */
Cell.prototype.getContentSize = function () {
  return this.content.trim().length
}
 
/**
 * Generate a markdown formated Cell
 *
 * @param {String} align A Column aligment constants
 * @param {String} size  Pad result to this size
 * @return {String}
 */
Cell.prototype.toMarkdown = function (align, size) {
  var content = this.content.trim()
  size = size || content.length
 
  switch (align) {
    case Column.ALIGN_RIGHT:
      // content = pad(content, size, ' ', 'left')
      content = padleft(content, size, ' ')
      break
    case Column.ALIGN_CENTER:
      content = pad(content, size, ' ')
      break
    default:
      // content = pad(content, size, ' ', 'right')
      content = padright(content, size, ' ')
  }
  return ' ' + (content || ' ') + ' '
}