All files MultilineRow.js

72% Statements 18/25
25% Branches 1/4
57.14% Functions 4/7
72% Lines 18/25
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               7x                 1x 1x   1x 3x         1x 1x 1x 1x 1x 1x 1x     1x   1x                       1x                          
// Import
var repeat = require('lodash.repeat')
var Row = require('./Row')
 
// Export
module.exports = MultilineRow
 
/**
 * A table multiline row (a controlled list of basic row)
 *
 * @param {Array} rows An optional array of Row objects
 */
function MultilineRow (rows) {
  this.rows = rows || []
}
 
/**
 * Generate a markdown representation of this Row
 * @param {Colgroup} colgroup
 *        A Colgroup object (provide column size and aligment informations)
 * @return {String}
 */
MultilineRow.prototype.toMarkdown = function (colgroup) {
  var out = []
 
  this.rows.forEach(function (row) {
    out.push(row.toMarkdown(colgroup))
  })
 
  // Multiline splitter use the same column spaning than
  // it cell (but this is only a visual effect)
  var splitter = '|'
  var index = 0
  this.rows[0].cells.forEach(function (cell) {
    var size = colgroup.getFormatedSizeAt(index, cell.colspan)
    splitter += ' ' + repeat('-', size) + ' '
    splitter += repeat('|', cell.colspan)
    index += cell.colspan
  })
 
  out.push(splitter)
 
  return out.join('\n')
}
 
/**
 * Compact MultilineRow to a basic Row object
 *
 * Note that cells content now contain line-return: you can note use this
 * row for markdown output without reformating
 * (but this may be usefull for html output)
 *
 * @return {String}
 */
MultilineRow.prototype.toRow = function () {
  var newRow = new Row()
  this.rows.forEach(function (row) {
    row.cells.forEach(function (cell, index) {
      if (!newRow.cells[index]) {
        newRow.cells[index] = cell.clone()
      } else {
        newRow.cells[index].content += '\n' + cell.content
      }
    })
  })
  return newRow
}