Last active
September 5, 2022 15:24
-
-
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
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
/** | |
* 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