Created
October 8, 2020 13:55
-
-
Save aminnairi/ed2159262d185edf65c477f334483cfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asciiTable = text => { | |
const rows = text.split("\n"); | |
const nonEmptyRows = rows.filter(row => 0 !== row.trim().length); | |
const normalizedRows = nonEmptyRows.map(row => row.trim()); | |
const rowsColumns = normalizedRows.map(row => row.split("|")); | |
const initialPadding = []; | |
const padding = rowsColumns.reduce((pad, row) => row.map((column, index) => { | |
if ("undefined" === typeof pad[index]) { | |
return column.length; | |
} | |
if (pad[index] < column.length) { | |
return column.length; | |
} | |
return pad[index]; | |
}), initialPadding); | |
const rowsColumnsPadded = rowsColumns.map(row => row.map((column, index) => column.padEnd(padding[index]))); | |
const rowsPadded = rowsColumnsPadded.map(row => row.join(" | ")); | |
const table = rowsPadded.join("\n"); | |
return table; | |
}; | |
// [14, 5, 38] | |
const text = ` | |
Command|Alias|Description|Additional Information | |
make build|N/A.|Compile the program for your platform.|This does not work | |
make clean|N/A.|Clean the compiled artifacts.|This works | |
make install|make|Install the program.|Maybe this will work | |
make uninstall|N/A.|Uninstall the program.|??? | |
`; | |
console.log(asciiTable(text)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment