]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/querystring/querystring-parse-simple.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / querystring / querystring-parse-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-parse-simple', function(Y) {
9
10 // @TODO this looks like we are requiring the user to extract the querystring
11 // portion of the url, which isn't good.  The majority use case will be to
12 // extract querystring from the document configured for this YUI instance.
13 // This should be the default if qs is not supplied.
14
15 /*global Y */
16 /**
17  * <p>Provides Y.QueryString.stringify method for converting objects to Query Strings.
18  * This is a simpler implementation than the full querystring-stringify.</p>
19  * <p>Because some things may require basic query string escaping functionality,
20  * this module provides the bare minimum functionality (decoding a hash of simple values),
21  * without the additional support for arrays, objects, and so on.</p>
22  * <p>This provides a friendly way to deserialize basic query strings, without necessitating
23  * a lot of code for simple use-cases.</p>
24  *
25  * @module querystring
26  * @submodule querystring-parse-simple
27  * @for QueryString
28  * @static
29  */
30
31 var QueryString = Y.namespace("QueryString");
32
33 /**
34  * Provides Y.QueryString.parse method to accept Query Strings and return native
35  * JavaScript objects.
36  *
37  * @module querystring
38  * @submodule querystring-parse
39  * @for QueryString
40  * @method parse
41  * @param qs {String} Querystring to be parsed into an object.
42  * @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
43  * @param eq  {String} (optional) Character that should join keys to their values. Default: "="
44  * @public
45  * @static
46  * @static
47  */
48 QueryString.parse = function (qs, sep, eq) {
49     sep = sep || "&";
50     eq = eq || "=";
51     for (
52         var obj = {},
53             i = 0,
54             pieces = qs.split(sep),
55             l = pieces.length,
56             tuple;
57         i < l;
58         i ++
59     ) {
60         tuple = pieces[i].split(eq);
61         if (tuple.length > 0) {
62             obj[QueryString.unescape(tuple.shift())] = QueryString.unescape(tuple.join(eq));
63         }
64     }
65     return obj;
66 };
67
68 /**
69  * Provides Y.QueryString.unescape method to be able to override default decoding
70  * method.  This is important in cases where non-standard delimiters are used, if
71  * the delimiters would not normally be handled properly by the builtin
72  * (en|de)codeURIComponent functions.
73  * Default: replace "+" with " ", and then decodeURIComponent behavior.
74  * @module querystring
75  * @submodule querystring-parse
76  * @for QueryString
77  * @method unescape
78  * @param s {String} String to be decoded.
79  * @public
80  * @static
81  **/
82 QueryString.unescape = function (s) {
83     return decodeURIComponent(s.replace(/\+/g, ' '));
84 };
85
86
87
88 }, '3.3.0' );