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