]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/querystring/querystring-stringify.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / querystring / querystring-stringify.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('querystring-stringify', function(Y) {
9
10 /**
11  * Provides Y.QueryString.stringify method for converting objects to Query Strings.
12  *
13  * @module querystring
14  * @submodule querystring-stringify
15  * @for QueryString
16  * @static
17  */
18
19 var QueryString = Y.namespace("QueryString"),
20     stack = [],
21     L = Y.Lang;
22
23 /**
24  * Provides Y.QueryString.escape method to be able to override default encoding
25  * method.  This is important in cases where non-standard delimiters are used, if
26  * the delimiters would not normally be handled properly by the builtin
27  * (en|de)codeURIComponent functions.
28  * Default: encodeURIComponent
29  * @module querystring
30  * @submodule querystring-stringify
31  * @for QueryString
32  * @static
33  **/
34 QueryString.escape = encodeURIComponent;
35
36 /**
37  * <p>Converts an arbitrary value to a Query String representation.</p>
38  *
39  * <p>Objects with cyclical references will trigger an exception.</p>
40  *
41  * @method stringify
42  * @public
43  * @param obj {Variant} any arbitrary value to convert to query string
44  * @param cfg {Object} (optional) Configuration object.  The three
45  * supported configurations are:
46  * <ul><li>sep: When defined, the value will be used as the key-value
47  * separator.  The default value is "&".</li>
48  * <li>eq: When defined, the value will be used to join the key to
49  * the value.  The default value is "=".</li>
50  * <li>arrayKey: When set to true, the key of an array will have the
51  * '[]' notation appended to the key.  The default value is false.
52  * </li></ul>
53  * @param name {String} (optional) Name of the current key, for handling children recursively.
54  * @static
55  */
56 QueryString.stringify = function (obj, c, name) {
57     var begin, end, i, l, n, s,
58         sep = c && c.sep ? c.sep : "&",
59         eq = c && c.eq ? c.eq : "=",
60         aK = c && c.arrayKey ? c.arrayKey : false;
61
62     if (L.isNull(obj) || L.isUndefined(obj) || L.isFunction(obj)) {
63         return name ? QueryString.escape(name) + eq : '';
64     }
65
66     if (L.isBoolean(obj) || Object.prototype.toString.call(obj) === '[object Boolean]') {
67         obj =+ obj;
68     }
69
70     if (L.isNumber(obj) || L.isString(obj)) {
71         return QueryString.escape(name) + eq + QueryString.escape(obj);
72     }
73
74     if (L.isArray(obj)) {
75         s = [];
76         name = aK ? name + '[]' : name;
77         l = obj.length;
78         for (i = 0; i < l; i++) {
79             s.push( QueryString.stringify(obj[i], c, name) );
80         }
81
82         return s.join(sep);
83     }
84     // now we know it's an object.
85
86     // Check for cyclical references in nested objects
87     for (i = stack.length - 1; i >= 0; --i) {
88         if (stack[i] === obj) {
89             throw new Error("QueryString.stringify. Cyclical reference");
90         }
91     }
92
93     stack.push(obj);
94     s = [];
95     begin = name ? name + '[' : '';
96     end = name ? ']' : '';
97     for (i in obj) {
98         if (obj.hasOwnProperty(i)) {
99             n = begin + i + end;
100             s.push(QueryString.stringify(obj[i], c, n));
101         }
102     }
103
104     stack.pop();
105     s = s.join(sep);
106     if (!s && name) {
107         return name + "=";
108     }
109
110     return s;
111 };
112
113
114
115 }, '3.3.0' );