[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/querystring-stringify/ -> querystring-stringify.js (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1