I have following function that display ASCII table
function ascii_table(array, header) {
if (!array.length) {
return \'\'
Here it is: https://jsfiddle.net/zww557sh/1/
function ascii_table(array, header) {
if (!array.length) {
return '';
}
var lengths = array[0].map(function(_, i) {
var col = array.map(function(row) {
if (row[i] != undefined) {
return row[i].length;
} else {
return 0;
}
});
return Math.max.apply(Math, col);
});
array = array.map(function(row, r) {
// let's split the table cells to lines
var cols = row.map(function(item, i){
return item.split("\n")
});
// and calculate the max column-height in this row
var maxH = (cols.reduce(function(i,j){return i.length > j.length ? i : j})).length;
// here we manually build the string that will represent
// this row of the table, including the "multi-line" cells
var rowStr = "";
// loop until the maximum cell height in this row
for (h=0;h0) rowStr += " | ";
// item is the h'th in column c, but it might
// be non existing, then we use ""
var item = cols[c][h] || "";
var size = item.length;
// here we use the same padding
if (size < lengths[c]) {
item += new Array(lengths[c]-size+1).join(' ');
}
rowStr += item;
}
rowStr += " |";
if (h
Output:
+---------------------+------+------------+-----+-----------------------+------------+---------------------+
| date | nick | email | www | comment | ip | avatar |
+---------------------+------+------------+-----+-----------------------+------------+---------------------+
| 2016-01-28 11:40:59 | lol | lol@lol.fr | | nocomment | 1844311719 | avatars/default.png |
| | | | | lol | | |
| | | | | lol | | |
| | | | | lol | | |
| 2016-01-10 15:13:59 | ehs | what | | ente rm comment. | 1423172924 | avatars/default.png |
+---------------------+------+------------+-----+-----------------------+------------+---------------------+