-
-
Save micahblu/1d05386183365740fabf3601eef49cfa to your computer and use it in GitHub Desktop.
console log print method, for cases where console log doesn't expand consoled objects
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 print = (obj, log=true) => { | |
let tab = ` `; | |
let tabs = [tab]; | |
let output = ''; | |
const quotify = (value) => typeof value === 'string' ? `"${value}"` : value; | |
const _print = (_obj) => { | |
let output = ''; | |
for (let prop in _obj) { | |
if (!_obj.hasOwnProperty(prop)) continue; | |
if (typeof _obj[prop] === 'object') { | |
tabs.push(tab); | |
output += tabs.slice(1).join('') + `${prop}` + ': ' + '{\n' + _print(_obj[prop]); | |
} else { | |
output += tabs.slice(1).join('') + tab + `${prop}` + ': ' + quotify(_obj[prop]) + '\n'; | |
} | |
} | |
tabs.pop(); | |
output += tabs.join('') + '}\n'; | |
return output; | |
}; | |
output = '{\n'+_print(obj); | |
if (log) console.log(output); | |
else return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment