PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/yui/3.5.0pr1/querystring-stringify/querystring-stringify-debug.js

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