]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/querystring/querystring-stringify-simple.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / querystring / querystring-stringify-simple.js
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.3.0
6 build: 3167
7 */
8 YUI.add('querystring-stringify-simple', function(Y) {
9
10 /*global Y */
11 /**
12  * <p>Provides Y.QueryString.stringify method for converting objects to Query Strings.
13  * This is a subset implementation of the full querystring-stringify.</p>
14  * <p>This module provides the bare minimum functionality (encoding a hash of simple values),
15  * without the additional support for nested data structures.  Every key-value pair is
16  * encoded by encodeURIComponent.</p>
17  * <p>This module provides a minimalistic way for io to handle  single-level objects
18  * as transaction data.</p>
19  *
20  * @module querystring
21  * @submodule querystring-stringify-simple
22  * @for QueryString
23  * @static
24  */
25
26 var QueryString = Y.namespace("QueryString"),
27     EUC = encodeURIComponent;
28
29 /**
30  * <p>Converts a simple object to a Query String representation.</p>
31  * <p>Nested objects, Arrays, and so on, are not supported.</p>
32  *
33  * @method stringify
34  * @for QueryString
35  * @public
36  * @submodule querystring-stringify-simple
37  * @param obj {Object} A single-level object to convert to a querystring.
38  * @param cfg {Object} (optional) Configuration object.  In the simple
39  *                                module, only the arrayKey setting is
40  *                                supported.  When set to true, the key of an
41  *                                array will have the '[]' notation appended
42  *                                to the key;.
43  * @static
44  */
45 QueryString.stringify = function (obj, c) {
46     var qs = [],
47         // Default behavior is false; standard key notation.
48         s = c && c.arrayKey ? true : false,
49         key, i, l;
50
51     for (key in obj) {
52         if (obj.hasOwnProperty(key)) {
53             if (Y.Lang.isArray(obj[key])) {
54                 for (i = 0, l = obj[key].length; i < l; i++) {
55                     qs.push(EUC(s ? key + '[]' : key) + '=' + EUC(obj[key][i]));
56                 }
57             }
58             else {
59                 qs.push(EUC(key) + '=' + EUC(obj[key]));
60             }
61         }
62     }
63
64     return qs.join('&');
65 };
66
67
68
69 }, '3.3.0' );