Dashboard sipadu mbip
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

querystring-stringify.js 3.2KB

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