Skip to content

Instantly share code, notes, and snippets.

@micahblu
Last active October 3, 2018 15:56
Show Gist options
  • Save micahblu/1d05386183365740fabf3601eef49cfa to your computer and use it in GitHub Desktop.
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
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