simple-string-table
Version:
it allows to create a simple impression of a table based on an array
24 lines (23 loc) • 511 B
JavaScript
module.exports = function table(rows, padding = 2) {
let spaces = rows.reduce(
(spaces, row) =>
row.reduce((spaces, value, index) => {
value = "" + value;
if (spaces[index] < value.length || spaces[index] == null) {
spaces[index] = value.length;
}
return spaces;
}, spaces),
[]
);
return rows
.map(row =>
row
.map(
(value, index) =>
value + " ".repeat(spaces[index] - value.length + padding)
)
.join("")
)
.join("\n");
};