Skip to content

Instantly share code, notes, and snippets.

@aminnairi
Created October 8, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminnairi/ed2159262d185edf65c477f334483cfb to your computer and use it in GitHub Desktop.
Save aminnairi/ed2159262d185edf65c477f334483cfb to your computer and use it in GitHub Desktop.
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