Created
October 6, 2014 21:06
-
-
Save yonester/ffa46f209b60b8d92755 to your computer and use it in GitHub Desktop.
Super simple pretty print
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
var _ = require('lodash'); | |
// Pretty print for JSON-style objects. | |
function pprint(obj) { | |
if (!pprint.indent) pprint.indent = ''; | |
if (_.isPlainObject(obj)) { | |
_.forOwn(obj, function(v, k) { | |
if (_.isArray(v) || _.isPlainObject(v)) { | |
console.log(pprint.indent + k + ':'); | |
pprint.indent += ' '; | |
pprint(v); | |
pprint.indent = pprint.indent.slice(0, -2); | |
} else { | |
console.log(pprint.indent + k + ': ' + v); | |
} | |
}); | |
} else if (_.isArray(obj)) { | |
_.each(obj, pprint); | |
} else { | |
console.log(pprint.indent + obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment