| 52 | }; |
| 53 | |
| 54 | const table = (head, columns) => { |
| 55 | const rows = []; |
| 56 | const columnWidths = ArrayPrototypeMap(head, (h) => getStringWidth(h)); |
| 57 | const longestColumn = MathMaxApply(ArrayPrototypeMap(columns, (a) => |
| 58 | a.length)); |
| 59 | |
| 60 | for (let i = 0; i < head.length; i++) { |
| 61 | const column = columns[i]; |
| 62 | for (let j = 0; j < longestColumn; j++) { |
| 63 | if (rows[j] === undefined) |
| 64 | rows[j] = []; |
| 65 | const value = rows[j][i] = |
| 66 | ObjectPrototypeHasOwnProperty(column, j) ? column[j] : ''; |
| 67 | const width = columnWidths[i] || 0; |
| 68 | const counted = getStringWidth(value); |
| 69 | columnWidths[i] = MathMax(width, counted); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const divider = ArrayPrototypeMap(columnWidths, (i) => |
| 74 | StringPrototypeRepeat(tableChars.middleMiddle, i + 2)); |
| 75 | |
| 76 | let result = tableChars.topLeft + |
| 77 | ArrayPrototypeJoin(divider, tableChars.topMiddle) + |
| 78 | tableChars.topRight + '\n' + |
| 79 | renderRow(head, columnWidths) + '\n' + |
| 80 | tableChars.leftMiddle + |
| 81 | ArrayPrototypeJoin(divider, tableChars.rowMiddle) + |
| 82 | tableChars.rightMiddle + '\n'; |
| 83 | |
| 84 | for (const row of rows) |
| 85 | result += `${renderRow(row, columnWidths)}\n`; |
| 86 | |
| 87 | result += tableChars.bottomLeft + |
| 88 | ArrayPrototypeJoin(divider, tableChars.bottomMiddle) + |
| 89 | tableChars.bottomRight; |
| 90 | |
| 91 | return result; |
| 92 | }; |
| 93 | |
| 94 | module.exports = table; |