|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('dump', function (Y, NAME) { |
|
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, |
|
22 OBJ = '{...}', |
|
23 FUN = 'f(){...}', |
|
24 COMMA = ', ', |
|
25 ARROW = ' => ', |
|
26 |
|
27 /** |
|
28 * Returns a simple string representation of the object or array. |
|
29 * Other types of objects will be returned unprocessed. Arrays |
|
30 * are expected to be indexed. |
|
31 * |
|
32 * @method dump |
|
33 * @param {Object} o The object to dump. |
|
34 * @param {Number} d How deep to recurse child objects, default 3. |
|
35 * @return {String} the dump result. |
|
36 * @for YUI |
|
37 */ |
|
38 dump = function(o, d) { |
|
39 var i, len, s = [], type = L.type(o); |
|
40 |
|
41 // Cast non-objects to string |
|
42 // Skip dates because the std toString is what we want |
|
43 // Skip HTMLElement-like objects because trying to dump |
|
44 // an element will cause an unhandled exception in FF 2.x |
|
45 if (!L.isObject(o)) { |
|
46 return o + ''; |
|
47 } else if (type == 'date') { |
|
48 return o; |
|
49 } else if (o.nodeType && o.tagName) { |
|
50 return o.tagName + '#' + o.id; |
|
51 } else if (o.document && o.navigator) { |
|
52 return 'window'; |
|
53 } else if (o.location && o.body) { |
|
54 return 'document'; |
|
55 } else if (type == 'function') { |
|
56 return FUN; |
|
57 } |
|
58 |
|
59 // dig into child objects the depth specifed. Default 3 |
|
60 d = (L.isNumber(d)) ? d : 3; |
|
61 |
|
62 // arrays [1, 2, 3] |
|
63 if (type == 'array') { |
|
64 s.push('['); |
|
65 for (i = 0, len = o.length; i < len; i = i + 1) { |
|
66 if (L.isObject(o[i])) { |
|
67 s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ); |
|
68 } else { |
|
69 s.push(o[i]); |
|
70 } |
|
71 s.push(COMMA); |
|
72 } |
|
73 if (s.length > 1) { |
|
74 s.pop(); |
|
75 } |
|
76 s.push(']'); |
|
77 // regexp /foo/ |
|
78 } else if (type == 'regexp') { |
|
79 s.push(o.toString()); |
|
80 // objects {k1 => v1, k2 => v2} |
|
81 } else { |
|
82 s.push('{'); |
|
83 for (i in o) { |
|
84 if (o.hasOwnProperty(i)) { |
|
85 try { |
|
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 } catch (e) { |
|
94 s.push('Error: ' + e.message); |
|
95 } |
|
96 } |
|
97 } |
|
98 if (s.length > 1) { |
|
99 s.pop(); |
|
100 } |
|
101 s.push('}'); |
|
102 } |
|
103 |
|
104 return s.join(''); |
|
105 }; |
|
106 |
|
107 Y.dump = dump; |
|
108 L.dump = dump; |
|
109 |
|
110 |
|
111 |
|
112 }, '3.10.3', {"requires": ["yui-base"]}); |