|
602
|
1 |
YUI.add('dump', function (Y, NAME) { |
|
|
2 |
|
|
|
3 |
/** |
|
|
4 |
* Returns a simple string representation of the object or array. |
|
|
5 |
* Other types of objects will be returned unprocessed. Arrays |
|
|
6 |
* are expected to be indexed. Use object notation for |
|
|
7 |
* associative arrays. |
|
|
8 |
* |
|
|
9 |
* If included, the dump method is added to the YUI instance. |
|
|
10 |
* |
|
|
11 |
* @module dump |
|
|
12 |
*/ |
|
|
13 |
|
|
|
14 |
var L = Y.Lang, |
|
|
15 |
OBJ = '{...}', |
|
|
16 |
FUN = 'f(){...}', |
|
|
17 |
COMMA = ', ', |
|
|
18 |
ARROW = ' => ', |
|
|
19 |
|
|
|
20 |
/** |
|
|
21 |
* Returns a simple string representation of the object or array. |
|
|
22 |
* Other types of objects will be returned unprocessed. Arrays |
|
|
23 |
* are expected to be indexed. |
|
|
24 |
* |
|
|
25 |
* @method dump |
|
|
26 |
* @param {Object} o The object to dump. |
|
|
27 |
* @param {Number} d How deep to recurse child objects, default 3. |
|
|
28 |
* @return {String} the dump result. |
|
|
29 |
* @for YUI |
|
|
30 |
*/ |
|
|
31 |
dump = function(o, d) { |
|
|
32 |
var i, len, s = [], type = L.type(o); |
|
|
33 |
|
|
|
34 |
// Cast non-objects to string |
|
|
35 |
// Skip dates because the std toString is what we want |
|
|
36 |
// Skip HTMLElement-like objects because trying to dump |
|
|
37 |
// an element will cause an unhandled exception in FF 2.x |
|
|
38 |
if (!L.isObject(o)) { |
|
|
39 |
return o + ''; |
|
|
40 |
} else if (type == 'date') { |
|
|
41 |
return o; |
|
|
42 |
} else if (o.nodeType && o.tagName) { |
|
|
43 |
return o.tagName + '#' + o.id; |
|
|
44 |
} else if (o.document && o.navigator) { |
|
|
45 |
return 'window'; |
|
|
46 |
} else if (o.location && o.body) { |
|
|
47 |
return 'document'; |
|
|
48 |
} else if (type == 'function') { |
|
|
49 |
return FUN; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
// dig into child objects the depth specifed. Default 3 |
|
|
53 |
d = (L.isNumber(d)) ? d : 3; |
|
|
54 |
|
|
|
55 |
// arrays [1, 2, 3] |
|
|
56 |
if (type == 'array') { |
|
|
57 |
s.push('['); |
|
|
58 |
for (i = 0, len = o.length; i < len; i = i + 1) { |
|
|
59 |
if (L.isObject(o[i])) { |
|
|
60 |
s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ); |
|
|
61 |
} else { |
|
|
62 |
s.push(o[i]); |
|
|
63 |
} |
|
|
64 |
s.push(COMMA); |
|
|
65 |
} |
|
|
66 |
if (s.length > 1) { |
|
|
67 |
s.pop(); |
|
|
68 |
} |
|
|
69 |
s.push(']'); |
|
|
70 |
// regexp /foo/ |
|
|
71 |
} else if (type == 'regexp') { |
|
|
72 |
s.push(o.toString()); |
|
|
73 |
// objects {k1 => v1, k2 => v2} |
|
|
74 |
} else { |
|
|
75 |
s.push('{'); |
|
|
76 |
for (i in o) { |
|
|
77 |
if (o.hasOwnProperty(i)) { |
|
|
78 |
try { |
|
|
79 |
s.push(i + ARROW); |
|
|
80 |
if (L.isObject(o[i])) { |
|
|
81 |
s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ); |
|
|
82 |
} else { |
|
|
83 |
s.push(o[i]); |
|
|
84 |
} |
|
|
85 |
s.push(COMMA); |
|
|
86 |
} catch (e) { |
|
|
87 |
s.push('Error: ' + e.message); |
|
|
88 |
} |
|
|
89 |
} |
|
|
90 |
} |
|
|
91 |
if (s.length > 1) { |
|
|
92 |
s.pop(); |
|
|
93 |
} |
|
|
94 |
s.push('}'); |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
return s.join(''); |
|
|
98 |
}; |
|
|
99 |
|
|
|
100 |
Y.dump = dump; |
|
|
101 |
L.dump = dump; |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
}, '@VERSION@', {"requires": ["yui-base"]}); |