3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('querystring-parse', function(Y) {
10
* The QueryString module adds support for serializing JavaScript objects into
11
* query strings and parsing JavaScript objects from query strings format.
13
* The QueryString namespace is added to your YUI instance including static methods
14
* `Y.QueryString.parse(..)` and `Y.QueryString.stringify(..)`.
16
* The `querystring` module is a alias for `querystring-parse` and
17
* `querystring-stringify`.
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.
29
* The QueryString module adds support for serializing JavaScript objects into
30
* query strings and parsing JavaScript objects from query strings format.
34
var QueryString = Y.namespace("QueryString"),
36
// Parse a key=val string.
37
// These can get pretty hairy
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) {
48
var sliced, numVal, head, tail, ret;
50
if (arguments.length !== 2) {
51
// key=val, called from the map/reduce
54
QueryString.unescape(key.shift()),
55
QueryString.unescape(key.join(eq))
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
64
if (val === numVal.toString(10)) {
69
sliced = /(.*)\[([^\]]*)\]$/.exec(key);
77
// ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
83
return parsePiece(head, [val]);
86
// obj: key[subkey]=val
89
return parsePiece(head, ret);
93
// the reducer function that merges each query piece together into one set of params
94
mergeParams = function(params, addition) {
96
// if it's uncontested, then just return the 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)
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]);
119
* Provides Y.QueryString.parse method to accept Query Strings and return native
120
* JavaScript objects.
122
* @module querystring
123
* @submodule querystring-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: "="
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(
136
qs.split(sep || "&"),
137
pieceParser(eq || "=")
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
154
* @param s {String} String to be decoded.
158
QueryString.unescape = function (s) {
159
return decodeURIComponent(s.replace(/\+/g, ' '));
165
}, '3.5.1' ,{requires:['array-extras', 'yui-base'], supersedes:['querystring-parse-simple']});