Those of you following me on Github may have noticed a recently added project called “prettyPrint“.
“prettyPrint” is an in-browser JavaScript “variable dumper” similar to ColdFusions’s cfdump
. It enables you to print out an object of any type in table format for viewing during debugging sessions. In combination with Firebug, “prettyPrint” will make you the best-equipped JavaScript debugger on earth! (not guaranteed)
Some of its key features:
- Entirely independent. It requires NO StyleSheets or images.
- Handles infinitely nested objects.
- All native JavaScript types are supported plus DOM nodes/elements!
- Protects against circular/repeated references.
- Allows you to specify the depth to which the object will be printed.
- Better browser users benefit from gradient column-headers! Thanks to HTML5 and
CANVAS
! - Allows low-level CSS customisation (if such a thing exists).
- Fully validated with JSLint!
I’ve recorded a short screencast demonstrating a couple of prettyPrint’s features:
“prettyPrint” is entirely independent. As mentioned, it requires NO StyleSheets or images. All styles are applied directly (inline) and are customizable via the exposed config
object (prettPrint.config.styles
).
Using it is simple enough:
var table = prettyPrint( anyRandomThing, { /*optional options object */ } ); // Making the table viewable is down to you... // e.g. document.body.appendChild(table); |
For more information visit the project’s page on Github!
PS. I’m aware that there’s an already existing “port” of ColdFusion’s cfdump
– I only created my one as a personal exercise; plus none of the ones I’ve seen so far have depth control or protection against circular references. 😉
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Great job. Might be interesting to see if prettyPrint can be integrated as a Firebug extension.
Pretty neat, James!
@Guillermo, Yep, this would definitely make a great Firebug extension. Although, ideally, something like this should be integrated into Firebug itself – the object overview currently available (
console.dir
) could probably be improved. Thanks for your comment Guillermo!@Jeffrey, thanks!
Good job,very useful.thx
Nice work James. Which color theme are you using in textmate?
That is excellent, James. It will be a great help in developing rich apps with JavaScript. Thanks very much.
What editor is being shown in the screencast?
The demo doesn’t work with Chrome though.
seems to be a very useful thing.
thanks for that
Thanks for the comments! 🙂
@Sebastian & @Mert, The editor is E-textEditor. The theme is “iPlastic”.
@Mandrake, Are you using Chrome v.2? Older versions (<2) of Chrome don't support
canvas.toDataURL
; if this is the case then it should work now, please try it and let me know. (I would test it myself but my version of Chrome is too new)Cool. I did this a while ago, but not pretty. I found it useful to include flags for ‘hasOwnProperty’ and ‘keys only’ (variable names)
It’s really helped.
prettyPrint throws errors if the function body is not wrapped in curly braces. Please fix this. For example,
prettyPrint(function() 1)
won’t work.Also, the whole thing gets messed up when there is are any parenthesis in the arguments. The arguments are also removed from their destructuring context. For example,
prettyPrint(function([a, b], {"(":c, d}) { return a+b+c+d })
putsa, b, c
in the arguments cell andc}) { return a + b + c;
in the body cell. If I remove the{ return
and}
, it puts nothing in the arguments cell and')': b}) b + 1;
in the body cell. If the"(":c
argument is replaced with")":c
, then prettyPrint thinks the arguments area, b,
, including that extra comma.Arguments can be very complex, so if there is any destructuring, it should be shown just like any object being passed to prettyPrint.
Thanks James, great job!
Awesome! This script goes directly to my debugger toolbox.
A tool like this is a must in a coder’s daily routine, thanks!
@Elijah, please forgive my ignorance but is that even “real” JavaScript? Glancing at the Rhino book and the ECMAScript specification I don’t see any mention of nested types as arguments in function definitions. I’ve never seen anything like
function([a, b], {”(”:c, d}) { return a+b+c+d }
in the wild… Could you link to some documentation or perhaps a specification detailing this?As for your first issue (
function() 1
), I’ve fixed this as best I can.Btw, you can force a function to be printed as an object like so:
James, JavaScript and ECMAScript are not the same thing. My code is valid JavaScript, not ECMAScript. JavaScript is just what SpiderMonkey and Rhino implement, as it’s owned by Mozilla. ECMAScript is a standard based on Mozilla’s JavaScript. Object destructuring in part of JavaScript 1.7 and implicit returns are part of JavaScript 1.8. Afaik, Opera supports basic object destructuring so things like
[a, b] = [b, a]
work but not advanced stuff.If you want to see some destructuring documentation (it’s pretty lacking though), refer to New in JavaScript 1.7 on MDC.
I also have another bug report:
prettyPrint throw errors when it is passed E4X XML objects. Unlike the other stuff I have mentioned, E4X actually is part of an Ecma standard (specifically, ECMA-357). Stuff like
prettyPrint(<foo><bar/><baz/></foo>)
should work after you fix the bug. This can be easily fixed by using the nativeprettyPrinting
in E4X usingXML.prettyPrinting
,XML.prettyIndent
, andXML.prototype.function::toXMLString
. It would involve temporarily changing the two prettyPrinting preferences temporarily and reverting them if needed.An alternative option for if you don’t want to support JavaScript: You could just fix only the last bug I mentioned and just rename this library to
.I forgot to mention, but I think that when prettyPrint detects a function using implicit returns, it should prepend “return ” to the body, as a normal function with a body of
1
is the same as a function without a body at all. For example, in SpiderMonkey,(function(){1}).toString() === (function(){}).toString()
.Nice work! very useful! Great! Great!