Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

querystring-stringify-debug.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Y.log("Number or string: "+obj);
  65. return QueryString.escape(name) + eq + QueryString.escape(obj);
  66. }
  67. if (L.isArray(obj)) {
  68. s = [];
  69. name = aK ? name + '[]' : name;
  70. l = obj.length;
  71. for (i = 0; i < l; i++) {
  72. s.push( QueryString.stringify(obj[i], c, name) );
  73. }
  74. return s.join(sep);
  75. }
  76. // now we know it's an object.
  77. // Y.log(
  78. // typeof obj + (typeof obj === 'object' ? " ok" : "ONOES!")+
  79. // Object.prototype.toString.call(obj)
  80. // );
  81. // Check for cyclical references in nested objects
  82. for (i = stack.length - 1; i >= 0; --i) {
  83. if (stack[i] === obj) {
  84. throw new Error("QueryString.stringify. Cyclical reference");
  85. }
  86. }
  87. stack.push(obj);
  88. s = [];
  89. begin = name ? name + '[' : '';
  90. end = name ? ']' : '';
  91. for (i in obj) {
  92. if (obj.hasOwnProperty(i)) {
  93. n = begin + i + end;
  94. s.push(QueryString.stringify(obj[i], c, n));
  95. }
  96. }
  97. stack.pop();
  98. s = s.join(sep);
  99. if (!s && name) {
  100. return name + "=";
  101. }
  102. return s;
  103. };
  104. }, '3.4.0' ,{requires:['yui-base']});