title: ASCII table where field can have newlines
body: <p>I have following function that display ASCII table</p>
  
  <pre><code>        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 &lt; 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;
              }
          }
  </code></pre>
  
  <p>the problem is when the cell contain new line I end up with something like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>+---------------------+--------+-------+-----+-------------------------------+------------+---------------------+
  | date                | nick   | email | www | comment                       | ip         | avatar              |
  +---------------------+--------+-------+-----+-------------------------------+------------+---------------------+
  | 2016-01-27 21:11:10 | stefan | kanev |     | dsfdsfsd
  sdfsdf
  sdfsdf
  sdfdsf | 1308240552 | avatars/default.png |
  +---------------------+--------+-------+-----+-------------------------------+------------+---------------------+
  </code></pre>
  
  <p>What should I change in my function to produce the result like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>+---------------------+--------+-------+-----+----------+------------+---------------------+
  | date                | nick   | email | www | comment  | ip         | avatar              |
  +---------------------+--------+-------+-----+----------+------------+---------------------+
  | 2016-01-27 21:11:10 | stefan | kanev |     | dsfdsfsd | 1308240552 | avatars/default.png |
  |                     |        |       |     | sdfsdf   |            |                     |
  |                     |        |       |     | sdfsdf   |            |                     |
  |                     |        |       |     | sdfdsf   |            |                     |
  +---------------------+--------+-------+-----+----------+------------+---------------------+
  </code></pre>