]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/dump/dump.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / dump / dump.js
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.0
6 build: 1549
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") {
55             return o;
56         } else if (o.nodeType && o.tagName) {
57             return o.tagName + '#' + o.id;
58         } else if (o.document && o.navigator) {
59             return 'window';
60         } else if (o.location && o.body) {
61             return 'document';
62         } else if (type == "function") {
63             return FUN;
64         }
65
66         // dig into child objects the depth specifed. Default 3
67         d = (L.isNumber(d)) ? d : 3;
68
69         // arrays [1, 2, 3]
70         if (type == "array") {
71             s.push("[");
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);
75                 } else {
76                     s.push(o[i]);
77                 }
78                 s.push(COMMA);
79             }
80             if (s.length > 1) {
81                 s.pop();
82             }
83             s.push("]");
84         // regexp /foo/
85         } else if (type == "regexp") {
86             s.push(o.toString());
87         // objects {k1 => v1, k2 => v2}
88         } else {
89             s.push("{");
90             for (i in o) {
91                 if (o.hasOwnProperty(i)) {
92                     try {
93                         s.push(i + ARROW);
94                         if (L.isObject(o[i])) {
95                             s.push((d > 0) ? L.dump(o[i], d-1) : OBJ);
96                         } else {
97                             s.push(o[i]);
98                         }
99                         s.push(COMMA);
100                     } catch(e) {
101                         s.push('Error: ' + e.message);
102                     }
103                 }
104             }
105             if (s.length > 1) {
106                 s.pop();
107             }
108             s.push("}");
109         }
110
111         return s.join("");
112     };
113
114     Y.dump = dump;
115     L.dump = dump;
116
117
118
119 }, '3.0.0' );