API Docs for: 3.18.1
Show:

File: querystring/js/querystring-stringify.js

  1. /**
  2. * Provides Y.QueryString.stringify method for converting objects to Query Strings.
  3. *
  4. * @module querystring
  5. * @submodule querystring-stringify
  6. */
  7.  
  8. var QueryString = Y.namespace("QueryString"),
  9. stack = [],
  10. L = Y.Lang;
  11.  
  12. /**
  13. * Provides Y.QueryString.escape method to be able to override default encoding
  14. * method. This is important in cases where non-standard delimiters are used, if
  15. * the delimiters would not normally be handled properly by the builtin
  16. * (en|de)codeURIComponent functions.
  17. * Default: encodeURIComponent
  18. *
  19. * @method escape
  20. * @for QueryString
  21. * @static
  22. **/
  23. QueryString.escape = encodeURIComponent;
  24.  
  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. * @for QueryString
  32. * @public
  33. * @param obj {Any} any arbitrary value to convert to query string
  34. * @param cfg {Object} (optional) Configuration object. The three
  35. * supported configurations are:
  36. * <ul><li>sep: When defined, the value will be used as the key-value
  37. * separator. The default value is "&".</li>
  38. * <li>eq: When defined, the value will be used to join the key to
  39. * the value. The default value is "=".</li>
  40. * <li>arrayKey: When set to true, the key of an array will have the
  41. * '[]' notation appended to the key. The default value is false.
  42. * </li></ul>
  43. * @param name {String} (optional) Name of the current key, for handling children recursively.
  44. * @static
  45. */
  46. QueryString.stringify = function (obj, c, name) {
  47. var begin, end, i, l, n, s,
  48. sep = c && c.sep ? c.sep : "&",
  49. eq = c && c.eq ? c.eq : "=",
  50. aK = c && c.arrayKey ? c.arrayKey : false;
  51.  
  52. if (L.isNull(obj) || L.isUndefined(obj) || L.isFunction(obj)) {
  53. return name ? QueryString.escape(name) + eq : '';
  54. }
  55.  
  56. if (L.isBoolean(obj) || Object.prototype.toString.call(obj) === '[object Boolean]') {
  57. obj =+ obj;
  58. }
  59.  
  60. if (L.isNumber(obj) || L.isString(obj)) {
  61. // Y.log("Number or string: "+obj);
  62. return QueryString.escape(name) + eq + QueryString.escape(obj);
  63. }
  64.  
  65. if (L.isArray(obj)) {
  66. s = [];
  67. name = aK ? name + '[]' : name;
  68. l = obj.length;
  69. for (i = 0; i < l; i++) {
  70. s.push( QueryString.stringify(obj[i], c, name) );
  71. }
  72.  
  73. return s.join(sep);
  74. }
  75. // now we know it's an object.
  76. // Y.log(
  77. // typeof obj + (typeof obj === 'object' ? " ok" : "ONOES!")+
  78. // Object.prototype.toString.call(obj)
  79. // );
  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.  
  88. stack.push(obj);
  89. s = [];
  90. begin = name ? name + '[' : '';
  91. end = name ? ']' : '';
  92. for (i in obj) {
  93. if (obj.hasOwnProperty(i)) {
  94. n = begin + i + end;
  95. s.push(QueryString.stringify(obj[i], c, n));
  96. }
  97. }
  98.  
  99. stack.pop();
  100. s = s.join(sep);
  101. if (!s && name) {
  102. return name + "=";
  103. }
  104.  
  105. return s;
  106. };
  107.