~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/querystring-parse/querystring-parse.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('querystring-parse', function(Y) {
 
8
 
 
9
/**
 
10
 * The QueryString module adds support for serializing JavaScript objects into
 
11
 * query strings and parsing JavaScript objects from query strings format.
 
12
 *
 
13
 * The QueryString namespace is added to your YUI instance including static methods
 
14
 * `Y.QueryString.parse(..)` and `Y.QueryString.stringify(..)`.
 
15
 *
 
16
 * The `querystring` module is a alias for `querystring-parse` and
 
17
 * `querystring-stringify`.
 
18
 *
 
19
 * As their names suggest, `querystring-parse` adds support for parsing
 
20
 * Query String data (`Y.QueryString.parse`) and `querystring-stringify` for serializing
 
21
 * JavaScript data into Query Strings (`Y.QueryString.stringify`).  You may choose to
 
22
 * include either of the submodules individually if you don't need the
 
23
 * complementary functionality, or include the rollup for both.
 
24
 *
 
25
 * @module querystring
 
26
 * @main querystring
 
27
*/
 
28
/**
 
29
 * The QueryString module adds support for serializing JavaScript objects into
 
30
 * query strings and parsing JavaScript objects from query strings format.
 
31
 * @class QueryString
 
32
 * @static
 
33
 */
 
34
var QueryString = Y.namespace("QueryString"),
 
35
 
 
36
// Parse a key=val string.
 
37
// These can get pretty hairy
 
38
// example flow:
 
39
// parse(foo[bar][][bla]=baz)
 
40
// return parse(foo[bar][][bla],"baz")
 
41
// return parse(foo[bar][], {bla : "baz"})
 
42
// return parse(foo[bar], [{bla:"baz"}])
 
43
// return parse(foo, {bar:[{bla:"baz"}]})
 
44
// return {foo:{bar:[{bla:"baz"}]}}
 
45
pieceParser = function (eq) {
 
46
    return function parsePiece (key, val) {
 
47
 
 
48
        var sliced, numVal, head, tail, ret;
 
49
 
 
50
        if (arguments.length !== 2) {
 
51
            // key=val, called from the map/reduce
 
52
            key = key.split(eq);
 
53
            return parsePiece(
 
54
                QueryString.unescape(key.shift()),
 
55
                QueryString.unescape(key.join(eq))
 
56
            );
 
57
        }
 
58
        key = key.replace(/^\s+|\s+$/g, '');
 
59
        if (Y.Lang.isString(val)) {
 
60
            val = val.replace(/^\s+|\s+$/g, '');
 
61
            // convert numerals to numbers
 
62
            if (!isNaN(val)) {
 
63
                numVal = +val;
 
64
                if (val === numVal.toString(10)) {
 
65
                    val = numVal;
 
66
                }
 
67
            }
 
68
        }
 
69
        sliced = /(.*)\[([^\]]*)\]$/.exec(key);
 
70
        if (!sliced) {
 
71
            ret = {};
 
72
            if (key) {
 
73
                ret[key] = val;
 
74
            }
 
75
            return ret;
 
76
        }
 
77
        // ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
 
78
        tail = sliced[2];
 
79
        head = sliced[1];
 
80
 
 
81
        // array: key[]=val
 
82
        if (!tail) {
 
83
            return parsePiece(head, [val]);
 
84
        }
 
85
 
 
86
        // obj: key[subkey]=val
 
87
        ret = {};
 
88
        ret[tail] = val;
 
89
        return parsePiece(head, ret);
 
90
    };
 
91
},
 
92
 
 
93
// the reducer function that merges each query piece together into one set of params
 
94
mergeParams = function(params, addition) {
 
95
    return (
 
96
        // if it's uncontested, then just return the addition.
 
97
        (!params) ? addition
 
98
        // if the existing value is an array, then concat it.
 
99
        : (Y.Lang.isArray(params)) ? params.concat(addition)
 
100
        // if the existing value is not an array, and either are not objects, arrayify it.
 
101
        : (!Y.Lang.isObject(params) || !Y.Lang.isObject(addition)) ? [params].concat(addition)
 
102
        // else merge them as objects, which is a little more complex
 
103
        : mergeObjects(params, addition)
 
104
    );
 
105
},
 
106
 
 
107
// Merge two *objects* together. If this is called, we've already ruled
 
108
// out the simple cases, and need to do the for-in business.
 
109
mergeObjects = function(params, addition) {
 
110
    for (var i in addition) {
 
111
        if (i && addition.hasOwnProperty(i)) {
 
112
            params[i] = mergeParams(params[i], addition[i]);
 
113
        }
 
114
    }
 
115
    return params;
 
116
};
 
117
 
 
118
/**
 
119
 * Provides Y.QueryString.parse method to accept Query Strings and return native
 
120
 * JavaScript objects.
 
121
 *
 
122
 * @module querystring
 
123
 * @submodule querystring-parse
 
124
 * @for QueryString
 
125
 * @method parse
 
126
 * @param qs {String} Querystring to be parsed into an object.
 
127
 * @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
 
128
 * @param eq  {String} (optional) Character that should join keys to their values. Default: "="
 
129
 * @public
 
130
 * @static
 
131
 */
 
132
QueryString.parse = function (qs, sep, eq) {
 
133
    // wouldn't Y.Array(qs.split()).map(pieceParser(eq)).reduce(mergeParams) be prettier?
 
134
    return Y.Array.reduce(
 
135
        Y.Array.map(
 
136
            qs.split(sep || "&"),
 
137
            pieceParser(eq || "=")
 
138
        ),
 
139
        {},
 
140
        mergeParams
 
141
    );
 
142
};
 
143
 
 
144
/**
 
145
 * Provides Y.QueryString.unescape method to be able to override default decoding
 
146
 * method.  This is important in cases where non-standard delimiters are used, if
 
147
 * the delimiters would not normally be handled properly by the builtin
 
148
 * (en|de)codeURIComponent functions.
 
149
 * Default: replace "+" with " ", and then decodeURIComponent behavior.
 
150
 * @module querystring
 
151
 * @submodule querystring-parse
 
152
 * @for QueryString
 
153
 * @method unescape
 
154
 * @param s {String} String to be decoded.
 
155
 * @public
 
156
 * @static
 
157
 **/
 
158
QueryString.unescape = function (s) {
 
159
    return decodeURIComponent(s.replace(/\+/g, ' '));
 
160
};
 
161
 
 
162
 
 
163
 
 
164
 
 
165
}, '3.5.1' ,{requires:['array-extras', 'yui-base'], supersedes:['querystring-parse-simple']});