PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/yui/3.4.0pr3/querystring/querystring-debug.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 270 lines | 122 code | 32 blank | 116 comment | 31 complexity | cf214d454ec406483a9536269b86b9cd MD5 | raw file
  1. YUI.add('querystring-parse', function(Y) {
  2. /**
  3. * <p>The QueryString module adds support for serializing JavaScript objects into
  4. * query strings and parsing JavaScript objects from query strings format.</p>
  5. *
  6. * <p>The QueryString namespace is added to your YUI instance including static methods
  7. * Y.QueryString.parse(..) and Y.QueryString.stringify(..).</p>
  8. *
  9. * <p>The <code>querystring</code> module is a rollup of <code>querystring-parse</code> and
  10. * <code>querystring-stringify</code>.</p>
  11. *
  12. * <p>As their names suggest, <code>querystring-parse</code> adds support for parsing
  13. * Query String data (Y.QueryString.parse) and <code>querystring-stringify</code> for serializing
  14. * JavaScript data into Query Strings (Y.QueryString.stringify). You may choose to
  15. * include either of the submodules individually if you don't need the
  16. * complementary functionality, or include the rollup for both.</p>
  17. *
  18. * @module querystring
  19. * @class QueryString
  20. * @static
  21. */
  22. var QueryString = Y.namespace("QueryString"),
  23. // Parse a key=val string.
  24. // These can get pretty hairy
  25. // example flow:
  26. // parse(foo[bar][][bla]=baz)
  27. // return parse(foo[bar][][bla],"baz")
  28. // return parse(foo[bar][], {bla : "baz"})
  29. // return parse(foo[bar], [{bla:"baz"}])
  30. // return parse(foo, {bar:[{bla:"baz"}]})
  31. // return {foo:{bar:[{bla:"baz"}]}}
  32. pieceParser = function (eq) {
  33. return function parsePiece (key, val) {
  34. var sliced, numVal, head, tail, ret;
  35. if (arguments.length !== 2) {
  36. // key=val, called from the map/reduce
  37. key = key.split(eq);
  38. return parsePiece(
  39. QueryString.unescape(key.shift()),
  40. QueryString.unescape(key.join(eq))
  41. );
  42. }
  43. key = key.replace(/^\s+|\s+$/g, '');
  44. if (Y.Lang.isString(val)) {
  45. val = val.replace(/^\s+|\s+$/g, '');
  46. // convert numerals to numbers
  47. if (!isNaN(val)) {
  48. numVal = +val;
  49. if (val === numVal.toString(10)) {
  50. val = numVal;
  51. }
  52. }
  53. }
  54. sliced = /(.*)\[([^\]]*)\]$/.exec(key);
  55. if (!sliced) {
  56. ret = {};
  57. if (key) {
  58. ret[key] = val;
  59. }
  60. return ret;
  61. }
  62. // ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
  63. tail = sliced[2];
  64. head = sliced[1];
  65. // array: key[]=val
  66. if (!tail) {
  67. return parsePiece(head, [val]);
  68. }
  69. // obj: key[subkey]=val
  70. ret = {};
  71. ret[tail] = val;
  72. return parsePiece(head, ret);
  73. };
  74. },
  75. // the reducer function that merges each query piece together into one set of params
  76. mergeParams = function(params, addition) {
  77. return (
  78. // if it's uncontested, then just return the addition.
  79. (!params) ? addition
  80. // if the existing value is an array, then concat it.
  81. : (Y.Lang.isArray(params)) ? params.concat(addition)
  82. // if the existing value is not an array, and either are not objects, arrayify it.
  83. : (!Y.Lang.isObject(params) || !Y.Lang.isObject(addition)) ? [params].concat(addition)
  84. // else merge them as objects, which is a little more complex
  85. : mergeObjects(params, addition)
  86. );
  87. },
  88. // Merge two *objects* together. If this is called, we've already ruled
  89. // out the simple cases, and need to do the for-in business.
  90. mergeObjects = function(params, addition) {
  91. for (var i in addition) {
  92. if (i && addition.hasOwnProperty(i)) {
  93. params[i] = mergeParams(params[i], addition[i]);
  94. }
  95. }
  96. return params;
  97. };
  98. /**
  99. * Provides Y.QueryString.parse method to accept Query Strings and return native
  100. * JavaScript objects.
  101. *
  102. * @module querystring
  103. * @submodule querystring-parse
  104. * @for QueryString
  105. * @method parse
  106. * @param qs {String} Querystring to be parsed into an object.
  107. * @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
  108. * @param eq {String} (optional) Character that should join keys to their values. Default: "="
  109. * @public
  110. * @static
  111. */
  112. QueryString.parse = function (qs, sep, eq) {
  113. // wouldn't Y.Array(qs.split()).map(pieceParser(eq)).reduce(mergeParams) be prettier?
  114. return Y.Array.reduce(
  115. Y.Array.map(
  116. qs.split(sep || "&"),
  117. pieceParser(eq || "=")
  118. ),
  119. {},
  120. mergeParams
  121. );
  122. };
  123. /**
  124. * Provides Y.QueryString.unescape method to be able to override default decoding
  125. * method. This is important in cases where non-standard delimiters are used, if
  126. * the delimiters would not normally be handled properly by the builtin
  127. * (en|de)codeURIComponent functions.
  128. * Default: replace "+" with " ", and then decodeURIComponent behavior.
  129. * @module querystring
  130. * @submodule querystring-parse
  131. * @for QueryString
  132. * @method unescape
  133. * @param s {String} String to be decoded.
  134. * @public
  135. * @static
  136. **/
  137. QueryString.unescape = function (s) {
  138. return decodeURIComponent(s.replace(/\+/g, ' '));
  139. };
  140. }, '@VERSION@' ,{requires:['array-extras', 'yui-base']});
  141. YUI.add('querystring-stringify', function(Y) {
  142. /**
  143. * Provides Y.QueryString.stringify method for converting objects to Query Strings.
  144. *
  145. * @module querystring
  146. * @submodule querystring-stringify
  147. * @for QueryString
  148. * @static
  149. */
  150. var QueryString = Y.namespace("QueryString"),
  151. stack = [],
  152. L = Y.Lang;
  153. /**
  154. * Provides Y.QueryString.escape method to be able to override default encoding
  155. * method. This is important in cases where non-standard delimiters are used, if
  156. * the delimiters would not normally be handled properly by the builtin
  157. * (en|de)codeURIComponent functions.
  158. * Default: encodeURIComponent
  159. * @module querystring
  160. * @submodule querystring-stringify
  161. * @for QueryString
  162. * @static
  163. **/
  164. QueryString.escape = encodeURIComponent;
  165. /**
  166. * <p>Converts an arbitrary value to a Query String representation.</p>
  167. *
  168. * <p>Objects with cyclical references will trigger an exception.</p>
  169. *
  170. * @method stringify
  171. * @public
  172. * @param obj {Variant} any arbitrary value to convert to query string
  173. * @param cfg {Object} (optional) Configuration object. The three
  174. * supported configurations are:
  175. * <ul><li>sep: When defined, the value will be used as the key-value
  176. * separator. The default value is "&".</li>
  177. * <li>eq: When defined, the value will be used to join the key to
  178. * the value. The default value is "=".</li>
  179. * <li>arrayKey: When set to true, the key of an array will have the
  180. * '[]' notation appended to the key. The default value is false.
  181. * </li></ul>
  182. * @param name {String} (optional) Name of the current key, for handling children recursively.
  183. * @static
  184. */
  185. QueryString.stringify = function (obj, c, name) {
  186. var begin, end, i, l, n, s,
  187. sep = c && c.sep ? c.sep : "&",
  188. eq = c && c.eq ? c.eq : "=",
  189. aK = c && c.arrayKey ? c.arrayKey : false;
  190. if (L.isNull(obj) || L.isUndefined(obj) || L.isFunction(obj)) {
  191. return name ? QueryString.escape(name) + eq : '';
  192. }
  193. if (L.isBoolean(obj) || Object.prototype.toString.call(obj) === '[object Boolean]') {
  194. obj =+ obj;
  195. }
  196. if (L.isNumber(obj) || L.isString(obj)) {
  197. // Y.log("Number or string: "+obj);
  198. return QueryString.escape(name) + eq + QueryString.escape(obj);
  199. }
  200. if (L.isArray(obj)) {
  201. s = [];
  202. name = aK ? name + '[]' : name;
  203. l = obj.length;
  204. for (i = 0; i < l; i++) {
  205. s.push( QueryString.stringify(obj[i], c, name) );
  206. }
  207. return s.join(sep);
  208. }
  209. // now we know it's an object.
  210. // Y.log(
  211. // typeof obj + (typeof obj === 'object' ? " ok" : "ONOES!")+
  212. // Object.prototype.toString.call(obj)
  213. // );
  214. // Check for cyclical references in nested objects
  215. for (i = stack.length - 1; i >= 0; --i) {
  216. if (stack[i] === obj) {
  217. throw new Error("QueryString.stringify. Cyclical reference");
  218. }
  219. }
  220. stack.push(obj);
  221. s = [];
  222. begin = name ? name + '[' : '';
  223. end = name ? ']' : '';
  224. for (i in obj) {
  225. if (obj.hasOwnProperty(i)) {
  226. n = begin + i + end;
  227. s.push(QueryString.stringify(obj[i], c, n));
  228. }
  229. }
  230. stack.pop();
  231. s = s.join(sep);
  232. if (!s && name) {
  233. return name + "=";
  234. }
  235. return s;
  236. };
  237. }, '@VERSION@' ,{requires:['yui-base']});
  238. YUI.add('querystring', function(Y){}, '@VERSION@' ,{use:['querystring-parse', 'querystring-stringify']});