javascript - ASCII table where field can have newlines -


i have following function display ascii table

        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) {                 return '| ' + row.map(function(item, i) {                     var size = item.length;                     if (size < lengths[i]) {                         item += new array(lengths[i]-size+1).join(' ');                     }                     return item;                 }).join(' | ') + ' |';             });             var sep = '+' + lengths.map(function(length) {                 return new array(length+3).join('-');             }).join('+') + '+';             if (header) {                 return sep + '\n' + array[0] + '\n' + sep + '\n' +                     array.slice(1).join('\n') + '\n' + sep;             } else {                 return sep + '\n' + array.join('\n') + '\n' + sep;             }         } 

the problem when cell contain new line end this:

+---------------------+--------+-------+-----+-------------------------------+------------+---------------------+ | date                | nick   | email | www | comment                       | ip         | avatar              | +---------------------+--------+-------+-----+-------------------------------+------------+---------------------+ | 2016-01-27 21:11:10 | stefan | kanev |     | dsfdsfsd sdfsdf sdfsdf sdfdsf | 1308240552 | avatars/default.png | +---------------------+--------+-------+-----+-------------------------------+------------+---------------------+ 

what should change in function produce result this:

+---------------------+--------+-------+-----+----------+------------+---------------------+ | date                | nick   | email | www | comment  | ip         | avatar              | +---------------------+--------+-------+-----+----------+------------+---------------------+ | 2016-01-27 21:11:10 | stefan | kanev |     | dsfdsfsd | 1308240552 | avatars/default.png | |                     |        |       |     | sdfsdf   |            |                     | |                     |        |       |     | sdfsdf   |            |                     | |                     |        |       |     | sdfdsf   |            |                     | +---------------------+--------+-------+-----+----------+------------+---------------------+ 

you modify table , add new rows table before before rendering it:

function ascii_table(array, header) {     if (!array.length) {         return '';     }      //added     (var = array.length - 1; >= 0; i--) {         var row = array[i];         var stacks = [];         (var j = 0; j < row.length; j++) {             var newlines = row[j].split("\n");             row[j] = newlines.shift();             stacks.push(newlines);         }         var newrowscount = stacks.reduce(function(a, b) {             return a.length > b.length ? : b;         }).length;         (var k = newrowscount - 1; k >= 0; k--) {             array.splice(i + 1, 0, stacks.map(function(stackcolumn) {                 return stackcolumn[k] || "";             }));         }     }     //added      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) {         return '| ' + row.map(function(item, i) {             var size = item.length;             if (size < lengths[i]) {                 item += new array(lengths[i] - size + 1).join(' ');             }             return item;         }).join(' | ') + ' |';     });     var sep = '+' + lengths.map(function(length) {         return new array(length + 3).join('-');     }).join('+') + '+';     if (header) {         return sep + '\n' + array[0] + '\n' + sep + '\n' +             array.slice(1).join('\n') + '\n' + sep;     } else {         return sep + '\n' + array.join('\n') + '\n' + sep;     } } 

output:

+---------------------+------+--------+-----+------------------+------------+---------------------+ | date                | nick | email  | www | comment          | ip         | avatar              | +---------------------+------+--------+-----+------------------+------------+---------------------+ | 2016-01-28 11:40:59 | lol  | lol@lo |     | nocomment        | 1844311719 | avatars/default.png | |                     |      | l.fr   |     | lol              |            |                     | |                     |      |        |     | lol              |            |                     | |                     |      |        |     | lol              |            |                     | | 2016-01-10 15:13:59 | ehs  |   |     | ente rm comment. | 1423172924 | avatars/default.png | +---------------------+------+--------+-----+------------------+------------+---------------------+ 

(added new line in 1 email cell, test new lines in multiple columns).


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -