Máster en Ingeniería Informática @ Universidad de La Laguna
The Secret life of objets
Autores
TextCell
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;
UnderlLinedCell
const TextCell = require("./textcell.js");
class UnderlinedCell extends TextCell {
constructor(text) {
super(text);
}
get minHeight() {
let result = super.minHeight;
return super.minHeight + 1;
}
draw(width, height) {
return super.draw(width, height - 1).concat([super.repeat("-", width)]);
}
}
module.exports = UnderlinedCell;
RTextCell
const TextCell = require("./textcell.js");
class RTextCell extends TextCell {
constructor(text) {
super(text);
}
draw(width, height) {
var result = [];
for (var i = 0; i < height; i++) {
var line = this.text[i] || "";
result.push(super.repeat(" ", width - line.length) + line);
}
return result;
}
}
const {addMapClass, findClass} = require("./registry-class");
addMapClass("Number", RTextCell);
addMapClass("RTextCell", RTextCell);
module.exports = RTextCell;
Tabla
const TextCell = require("../src/textcell");
const UnderlinedCell = require("../src/underlinedcell");
const RTextCell = require("../src/rtextcell");
const StretchCell = require("../src/stretchcell");
const {addMapClass, findClass} = require('./registry-class');
colWidths = (filas) => {
return filas[0].map(function(_, i) {
return filas.reduce(function(max, row) {
return Math.max(max, row[i].minWidth);
}, 0);
});
};
colHeights = (filas) => {
return filas.map(function(row) {
return row.reduce(function(max, cell) {
return Math.max(max, cell.minHeight);
}, 0);
});
};
dataTable = (data) => {
var keys = Object.keys(data[0]);
var categorias = keys.map(function(categoria) {
return keys.map(function(categoria) {
return new UnderlinedCell(categoria.toString());
});
});
//Magia para evitar error de arrays, conversiones
categorias = categorias.splice(0, 1);
var datos = data.map(function(fila) {
return keys.map(function(i) {
let value = fila[i];
let {className, currClass, params} = findClass(value);
return new currClass(...params);
});
});
return categorias.concat(datos);
};
drawTable = (filas) => {
let width = colWidths(filas);
let height = colHeights(filas);
dibujarLinea = (datos, numeroLinea) => {
return datos.map(function(fila) {
return fila[numeroLinea];
}).join(" ");
};
dibujarFilas = (fila, numlinea) => {
var content = fila.map(function(elemento, numCol) {
return elemento.draw(width[numCol], height[numlinea]);
});
return content[0].map(function(_, nlinea) {
return dibujarLinea(content, nlinea);
}).join("\n");
}
return filas.map(dibujarFilas).join("\n");
};
createTable = (data) => { };
Main
const TextCell = require("./textcell");
const UnderlinedCell = require("./underlinedcell");
const DataTable = require("./tabla.js");
const fs = require("fs");
let index = 3;
if(index === process.argv.length) {
const input = require(process.argv[index -1]);
var table = dataTable(input);
console.log(drawTable(table));
} else {
throw Error("A file must be specified!");
}