|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('dump', function(Y) { |
|
9 |
|
10 /** |
|
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 |
|
14 * associative arrays. |
|
15 * |
|
16 * If included, the dump method is added to the YUI instance. |
|
17 * |
|
18 * @module dump |
|
19 */ |
|
20 |
|
21 var L=Y.Lang, OBJ='{...}', FUN='f(){...}', COMMA=', ', ARROW=' => ', |
|
22 |
|
23 /** |
|
24 * The following methods are added to the YUI instance |
|
25 * @class YUI~dump |
|
26 */ |
|
27 |
|
28 /** |
|
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 |
|
32 * associative arrays. |
|
33 * |
|
34 * @TODO dumping a window is causing an unhandled exception in |
|
35 * FireFox. |
|
36 * |
|
37 * This method is in the 'dump' module, which is not bundled with |
|
38 * the core YUI object |
|
39 * |
|
40 * @method dump |
|
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 |
|
44 */ |
|
45 dump = function(o, d) { |
|
46 var i, len, s = [], type = L.type(o); |
|
47 |
|
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 |
|
52 if (!L.isObject(o)) { |
|
53 return o + ""; |
|
54 } else if (type == "date" || ("nodeType" in o && "tagName" in o)) { |
|
55 return o; |
|
56 } else if (type == "function") { |
|
57 return FUN; |
|
58 } |
|
59 |
|
60 // dig into child objects the depth specifed. Default 3 |
|
61 d = (L.isNumber(d)) ? d : 3; |
|
62 |
|
63 // arrays [1, 2, 3] |
|
64 if (type == "array") { |
|
65 s.push("["); |
|
66 for (i=0,len=o.length;i<len;i=i+1) { |
|
67 if (L.isObject(o[i])) { |
|
68 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ); |
|
69 } else { |
|
70 s.push(o[i]); |
|
71 } |
|
72 s.push(COMMA); |
|
73 } |
|
74 if (s.length > 1) { |
|
75 s.pop(); |
|
76 } |
|
77 s.push("]"); |
|
78 // regexp /foo/ |
|
79 } else if (type == "regexp") { |
|
80 s.push(o.toString()); |
|
81 // objects {k1 => v1, k2 => v2} |
|
82 } else { |
|
83 s.push("{"); |
|
84 for (i in o) { |
|
85 if (o.hasOwnProperty(i)) { |
|
86 s.push(i + ARROW); |
|
87 if (L.isObject(o[i])) { |
|
88 s.push((d > 0) ? L.dump(o[i], d-1) : OBJ); |
|
89 } else { |
|
90 s.push(o[i]); |
|
91 } |
|
92 s.push(COMMA); |
|
93 } |
|
94 } |
|
95 if (s.length > 1) { |
|
96 s.pop(); |
|
97 } |
|
98 s.push("}"); |
|
99 } |
|
100 |
|
101 return s.join(""); |
|
102 }; |
|
103 |
|
104 Y.dump = dump; |
|
105 L.dump = dump; |
|
106 |
|
107 |
|
108 |
|
109 }, '3.0.0b1' ); |