all files / src/ textcell.js

100% Statements 18/18
100% Branches 2/2
100% Functions 5/5
100% Lines 18/18
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      74× 74×     37× 39×       41×     61× 61× 272×   61×     23× 23× 45× 45×   23×      
 
class TextCell {
  constructor(text){
    let txt = String(text);
    this.text = txt.split("\n");
  }
  get minWidth() {
    return this.text.reduce((width, line) => {
      return Math.max(width, line.length);
    }, 0);
  }
  get minHeight() {
    return this.text.length;
  }
  repeat(string, times) {
    let result = "";
    for(let i = 0; i < times; i++){
      result += string;
    }
    return result;
  }
  draw(width, height) {
    let result = [];
    for(let i = 0; i < height; i++){
      let line = this.text[i] || " ";
      result.push(line + this.repeat(" ", width - line.length));
    }
    return result;
  }
}
const { addMapClass, findClass} = require("./registry-class");
addMapClass("String", TextCell);
addMapClass("TextCell", TextCell);
module.exports = TextCell;