Skip to content

Instantly share code, notes, and snippets.

@lucamarogna
Last active September 5, 2022 15:24
Show Gist options
  • Save lucamarogna/e2628434ccb36ee90ad966604d0d42d8 to your computer and use it in GitHub Desktop.
Save lucamarogna/e2628434ccb36ee90ad966604d0d42d8 to your computer and use it in GitHub Desktop.
Reverse a console.table() output, in order to give back the original matrix
/**
* Given original matrix like this:
* [
* ['πŸ‰', 'πŸ’', '🍌'],
* ['🦁', 'πŸ¦„', '🐼'],
* ['πŸ•', '🍿', 'πŸ”'],
* ]
* console.table() prints:
* | (index) | 0 | 1 | 2 |
* --------------------------------
* | 0 | 'πŸ‰' | 'πŸ’' | '🍌' |
* | 1 | '🦁' | 'πŸ¦„' | '🐼' |
* | 2 | 'πŸ•' | '🍿' | 'πŸ”' |
* We would like to get the original array from this latter string.
*
* @param {string} str - The output of console.table()
* @return {string[][]} 2d array
*/
function fromConsoleTableTo2dArray(str) {
const res = [];
// break the text into an array of lines
let lines = str.split('\n');
// remove the first two lines (indexes)
lines.splice(0, 2);
for (const row of lines) {
let cols = row.trim().split('|');
// remove first column (indexes)
cols.splice(0, 2);
// remove last column (garbage)
cols.splice(cols.length - 1, 1)
const temp = [];
for (const col of cols) {
let modified = col.trim().replaceAll(`'`, '');
temp.push(modified)
}
res.push(temp);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment