querystring-stringify.js revision 76ca635d61eb3f9fb7c9d788a44fa8b1690aa138
474N/AYUI.add('querystring-stringify', function(Y) {
474N/A
474N/A/**
474N/A * Provides Y.QueryString.stringify method for converting objects to Query Strings.
474N/A *
474N/A * @module querystring
474N/A * @submodule querystring-stringify
474N/A * @for QueryString
474N/A * @static
474N/A */
474N/A
474N/Avar QueryString = Y.namespace("QueryString"),
474N/A stack = [],
474N/A L = Y.Lang;
474N/A
474N/A/**
474N/A * Provides Y.QueryString.escape method to be able to override default encoding
474N/A * method. This is important in cases where non-standard delimiters are used, if
474N/A * the delimiters would not normally be handled properly by the builtin
474N/A * (en|de)codeURIComponent functions.
474N/A * Default: encodeURIComponent
474N/A * @module querystring
3817N/A * @submodule querystring-stringify
474N/A * @for QueryString
474N/A * @static
474N/A **/
474N/AQueryString.escape = encodeURIComponent;
474N/A
474N/A/**
474N/A * <p>Converts an arbitrary value to a Query String representation.</p>
825N/A *
618N/A * <p>Objects with cyclical references will trigger an exception.</p>
474N/A *
474N/A * @method stringify
844N/A * @public
844N/A * @param obj {Variant} any arbitrary value to convert to query string
474N/A * @param cfg {Object} (optional) Configuration object. The three
1258N/A * supported configurations are:
474N/A * <ul><li>sep: When defined, the value will be used as the key-value
2899N/A * separator. The default value is "&".</li>
2899N/A * <li>eq: When defined, the value will be used to join the key to
474N/A * the value. The default value is "=".</li>
474N/A * <li>arrayKey: When set to true, the key of an array will have the
474N/A * '[]' notation appended to the key. The default value is false.
474N/A * </li></ul>
844N/A * @param name {String} (optional) Name of the current key, for handling children recursively.
844N/A * @static
474N/A */
474N/AQueryString.stringify = function (obj, c, name) {
474N/A var begin, end, i, l, n, s,
3817N/A sep = c && c.sep ? c.sep : "&",
3817N/A eq = c && c.eq ? c.eq : "=",
3817N/A aK = c && c.arrayKey ? c.arrayKey : false;
474N/A
474N/A if (L.isNull(obj) || L.isUndefined(obj) || L.isFunction(obj)) {
474N/A return name ? QueryString.escape(name) + eq : '';
474N/A }
474N/A
474N/A if (L.isBoolean(obj) || Object.prototype.toString.call(obj) === '[object Boolean]') {
474N/A obj =+ obj;
474N/A }
474N/A
474N/A if (L.isNumber(obj) || L.isString(obj)) {
474N/A return QueryString.escape(name) + eq + QueryString.escape(obj);
474N/A }
474N/A
474N/A if (L.isArray(obj)) {
474N/A s = [];
825N/A name = aK ? name + '[]' : name;
825N/A l = obj.length;
825N/A for (i = 0; i < l; i++) {
474N/A s.push( QueryString.stringify(obj[i], c, name) );
474N/A }
474N/A
474N/A return s.join(sep);
474N/A }
474N/A // now we know it's an object.
474N/A
474N/A // Check for cyclical references in nested objects
474N/A for (i = stack.length - 1; i >= 0; --i) {
4652N/A if (stack[i] === obj) {
474N/A throw new Error("QueryString.stringify. Cyclical reference");
474N/A }
474N/A }
474N/A
474N/A stack.push(obj);
474N/A s = [];
474N/A begin = name ? name + '[' : '';
4904N/A end = name ? ']' : '';
474N/A for (i in obj) {
474N/A if (obj.hasOwnProperty(i)) {
474N/A n = begin + i + end;
474N/A s.push(QueryString.stringify(obj[i], c, n));
474N/A }
474N/A }
474N/A
474N/A stack.pop();
1914N/A s = s.join(sep);
1914N/A if (!s && name) {
1914N/A return name + "=";
474N/A }
3477N/A
3477N/A return s;
474N/A};
474N/A
474N/A
474N/A}, '@VERSION@' ,{requires:['yui-base']});
474N/A