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