]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/dump/dump.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / dump / dump.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
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,
22         OBJ = '{...}',
23         FUN = 'f(){...}',
24         COMMA = ', ',
25         ARROW = ' => ',
26
27     /**
28      * The following methods are added to the YUI instance
29      * @class YUI~dump
30      */
31
32     /**
33      * Returns a simple string representation of the object or array.
34      * Other types of objects will be returned unprocessed.  Arrays
35      * are expected to be indexed.  Use object notation for
36      * associative arrays.
37      *
38      * This method is in the 'dump' module, which is not bundled with
39      * the core YUI object
40      *
41      * @method dump
42      * @param {object} o The object to dump.
43      * @param {int} d How deep to recurse child objects, default 3.
44      * @return {string} the dump result.
45      */
46     dump = function(o, d) {
47         var i, len, s = [], type = L.type(o);
48
49         // Cast non-objects to string
50         // Skip dates because the std toString is what we want
51         // Skip HTMLElement-like objects because trying to dump
52         // an element will cause an unhandled exception in FF 2.x
53         if (!L.isObject(o)) {
54             return o + '';
55         } else if (type == 'date') {
56             return o;
57         } else if (o.nodeType && o.tagName) {
58             return o.tagName + '#' + o.id;
59         } else if (o.document && o.navigator) {
60             return 'window';
61         } else if (o.location && o.body) {
62             return 'document';
63         } else if (type == 'function') {
64             return FUN;
65         }
66
67         // dig into child objects the depth specifed. Default 3
68         d = (L.isNumber(d)) ? d : 3;
69
70         // arrays [1, 2, 3]
71         if (type == 'array') {
72             s.push('[');
73             for (i = 0, len = o.length; i < len; i = i + 1) {
74                 if (L.isObject(o[i])) {
75                     s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
76                 } else {
77                     s.push(o[i]);
78                 }
79                 s.push(COMMA);
80             }
81             if (s.length > 1) {
82                 s.pop();
83             }
84             s.push(']');
85         // regexp /foo/
86         } else if (type == 'regexp') {
87             s.push(o.toString());
88         // objects {k1 => v1, k2 => v2}
89         } else {
90             s.push('{');
91             for (i in o) {
92                 if (o.hasOwnProperty(i)) {
93                     try {
94                         s.push(i + ARROW);
95                         if (L.isObject(o[i])) {
96                             s.push((d > 0) ? L.dump(o[i], d - 1) : OBJ);
97                         } else {
98                             s.push(o[i]);
99                         }
100                         s.push(COMMA);
101                     } catch (e) {
102                         s.push('Error: ' + e.message);
103                     }
104                 }
105             }
106             if (s.length > 1) {
107                 s.pop();
108             }
109             s.push('}');
110         }
111
112         return s.join('');
113     };
114
115     Y.dump = dump;
116     L.dump = dump;
117
118
119
120 }, '3.3.0' );