2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('dump', function(Y) {
11 * Returns a simple string representation of the object or array.
12 * Other types of objects will be returned unprocessed. Arrays
13 * are expected to be indexed. Use object notation for
16 * If included, the dump method is added to the YUI instance.
21 var L=Y.Lang, OBJ='{...}', FUN='f(){...}', COMMA=', ', ARROW=' => ',
24 * The following methods are added to the YUI instance
29 * Returns a simple string representation of the object or array.
30 * Other types of objects will be returned unprocessed. Arrays
31 * are expected to be indexed. Use object notation for
34 * @TODO dumping a window is causing an unhandled exception in
37 * This method is in the 'dump' module, which is not bundled with
41 * @param o {object} The object to dump
42 * @param d {int} How deep to recurse child objects, default 3
43 * @return {string} the dump result
45 dump = function(o, d) {
46 var i, len, s = [], type = L.type(o);
48 // Cast non-objects to string
49 // Skip dates because the std toString is what we want
50 // Skip HTMLElement-like objects because trying to dump
51 // an element will cause an unhandled exception in FF 2.x
54 } else if (type == "date") {
56 } else if (o.nodeType && o.tagName) {
57 return o.tagName + '#' + o.id;
58 } else if (o.document && o.navigator) {
60 } else if (o.location && o.body) {
62 } else if (type == "function") {
66 // dig into child objects the depth specifed. Default 3
67 d = (L.isNumber(d)) ? d : 3;
70 if (type == "array") {
72 for (i=0,len=o.length;i<len;i=i+1) {
73 if (L.isObject(o[i])) {
74 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
85 } else if (type == "regexp") {
87 // objects {k1 => v1, k2 => v2}
91 if (o.hasOwnProperty(i)) {
94 if (L.isObject(o[i])) {
95 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
101 s.push('Error: ' + e.message);