Last active
September 13, 2019 16:03
-
-
Save edwardgalligan/894fb167f194ce6335f398b87bc897ba to your computer and use it in GitHub Desktop.
print_r WIP :P
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 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