ASCII table 其中字段可以有换行符

ASCII table where field can have newlines

我有以下显示 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;
            }
        }

问题是当单元格包含新行时我最终得到这样的结果:

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

我应该在我的函数中更改什么才能产生这样的结果:

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

您可以在渲染之前修改 table 并将新行添加到 table:

function ascii_table(array, header) {
    if (!array.length) {
        return '';
    }

    //added
    for (var i = array.length - 1; i >= 0; i--) {
        var row = array[i];
        var stacks = [];
        for (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 ? a : b;
        }).length;
        for (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;
    }
}

输出:

+---------------------+------+--------+-----+------------------+------------+---------------------+
| 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  | what   |     | ente rm comment. | 1423172924 | avatars/default.png |
+---------------------+------+--------+-----+------------------+------------+---------------------+

(在一个电子邮件单元格中添加了新行,以测试多列中的新行)。

这可以在不对代码进行任何修改的情况下通过扩展 array -- 为注释的每一行添加一个新行来完成。第一行与之前相同——除了注释单元格仅包含注释的第一行。每个额外的行都将包含空单元格——除了注释单元格,它会包含该注释行。

为了便于阅读,我会分解大部分函数,​​但为了与您的编码风格保持一致,它可能看起来像这样:

  array = array.reduce(function (carry, originalRow) {
    var commentLines = originalRow[4].split(/\n/g);

    var rows = commentLines.map(function (commentLine, rowIndex) {
        var newRow = originalRow.map(function (originalCellContent, columnIndex) {
            var cellContent = '';

            if (rowIndex === 0) {
                cellContent = originalCellContent;
            }
            if (columnIndex === 4) {
                cellContent = commentLine;
            }

            return cellContent;
        });
        carry.push(newRow);
    });

    return carry;
  }, []);

您的 fiddle is here 的一个分支,添加了此代码。

这里是: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;h<maxH;h++) {
              rowStr += "| ";
              // loop on all the cells
              for (c=0;c<cols.length;c++) {
                if (c>0) 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<maxH-1) rowStr += "\n";
            }
          return rowStr;
        });
        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;
        }
    }

输出:

+---------------------+------+------------+-----+-----------------------+------------+---------------------+
| 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 |
+---------------------+------+------------+-----+-----------------------+------------+---------------------+