Skip to content

Instantly share code, notes, and snippets.

@edwardgalligan
Last active September 13, 2019 16:03
Show Gist options
  • Save edwardgalligan/894fb167f194ce6335f398b87bc897ba to your computer and use it in GitHub Desktop.
Save edwardgalligan/894fb167f194ce6335f398b87bc897ba to your computer and use it in GitHub Desktop.
print_r WIP :P
const printR = (obj, stringify = true) => {
let ret;
switch (true) {
case typeof obj === 'string':
case Number.isFinite(obj):
case Number.isNaN(obj):
case obj === null:
case typeof obj === 'undefined':
case typeof obj === 'boolean':
ret = obj;
break;
case typeof obj === 'function':
ret = obj.toString();
break;
case Array.isArray(obj):
ret = obj.map(item => printR(item, false));
break;
case /(Typed|Float\d+|(Big)?U?[iI]nt\d+)(Clamped)?Array/.test(obj.constructor.name):
ret = `[${obj.constructor.name}]`;
break;
case obj instanceof Map:
case obj instanceof Set:
ret = Array.from(obj);
break;
default:
ret = {};
Object.keys(obj).forEach(key => {
ret[key] = printR(obj[key], false);
});
}
return stringify ? JSON.stringify(ret) : ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment