Last active
May 24, 2019 20:44
-
-
Save matpratta/6290aaf3939cb70fab2a296cd358ea23 to your computer and use it in GitHub Desktop.
Returns a complete human-readable breakdown of a Javascript object
This file contains hidden or 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
| /** | |
| * debug_object (1.0.0) | |
| * Returns a complete human-readable breakdown of a Javascript object | |
| * Author: Matheus Pratta <https://github.com/matheusmk3> | |
| */ | |
| function debug_object (obj, braces, level) { | |
| // Recursion level | |
| if (!level) level = braces ? 1 : 0; | |
| // Identation | |
| var identation = ''; | |
| for (var i = 0; i < level; i++) { | |
| identation += '\t'; | |
| } | |
| // Output variable | |
| var out = ''; | |
| var hasProps = false; | |
| // Open | |
| if (braces) out += '{'; | |
| for (var key in obj) { | |
| // We have props | |
| hasProps = true; | |
| // Our current key value | |
| var value = obj[key]; | |
| // Check if we're dealing with an object | |
| if ( (typeof value === "object") && (value !== null) ) { | |
| // Print object | |
| out += '\n' + identation + key + ': ' + debug_object(value, braces, level + 1); | |
| } else { | |
| out += '\n' + identation + key + ': ' + JSON.stringify(value); | |
| } | |
| } | |
| // Close | |
| if (braces) out += ' }'; | |
| // Return processed output | |
| return out; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment