3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('querystring-parse', function(Y) {
10
* <p>The QueryString module adds support for serializing JavaScript objects into
11
* query strings and parsing JavaScript objects from query strings format.</p>
13
* <p>The QueryString namespace is added to your YUI instance including static methods
14
* Y.QueryString.parse(..) and Y.QueryString.stringify(..).</p>
16
* <p>The <code>querystring</code> module is a rollup of <code>querystring-parse</code> and
17
* <code>querystring-stringify</code>.</p>
19
* <p>As their names suggest, <code>querystring-parse</code> adds support for parsing
20
* Query String data (Y.QueryString.parse) and <code>querystring-stringify</code> 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.</p>
30
var QueryString = Y.namespace("QueryString"),
32
// Parse a key=val string.
33
// These can get pretty hairy
35
// parse(foo[bar][][bla]=baz)
36
// return parse(foo[bar][][bla],"baz")
37
// return parse(foo[bar][], {bla : "baz"})
38
// return parse(foo[bar], [{bla:"baz"}])
39
// return parse(foo, {bar:[{bla:"baz"}]})
40
// return {foo:{bar:[{bla:"baz"}]}}
41
pieceParser = function (eq) {
42
return function parsePiece (key, val) {
44
var sliced, numVal, head, tail, ret;
46
if (arguments.length !== 2) {
47
// key=val, called from the map/reduce
50
QueryString.unescape(key.shift()),
51
QueryString.unescape(key.join(eq))
54
key = key.replace(/^\s+|\s+$/g, '');
55
if (Y.Lang.isString(val)) {
56
val = val.replace(/^\s+|\s+$/g, '');
57
// convert numerals to numbers
60
if (val === numVal.toString(10)) {
65
sliced = /(.*)\[([^\]]*)\]$/.exec(key);
73
// ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
79
return parsePiece(head, [val]);
82
// obj: key[subkey]=val
85
return parsePiece(head, ret);
89
// the reducer function that merges each query piece together into one set of params
90
mergeParams = function(params, addition) {
92
// if it's uncontested, then just return the addition.
94
// if the existing value is an array, then concat it.
95
: (Y.Lang.isArray(params)) ? params.concat(addition)
96
// if the existing value is not an array, and either are not objects, arrayify it.
97
: (!Y.Lang.isObject(params) || !Y.Lang.isObject(addition)) ? [params].concat(addition)
98
// else merge them as objects, which is a little more complex
99
: mergeObjects(params, addition)
103
// Merge two *objects* together. If this is called, we've already ruled
104
// out the simple cases, and need to do the for-in business.
105
mergeObjects = function(params, addition) {
106
for (var i in addition) {
107
if (i && addition.hasOwnProperty(i)) {
108
params[i] = mergeParams(params[i], addition[i]);
115
* Provides Y.QueryString.parse method to accept Query Strings and return native
116
* JavaScript objects.
118
* @module querystring
119
* @submodule querystring-parse
122
* @param qs {String} Querystring to be parsed into an object.
123
* @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&"
124
* @param eq {String} (optional) Character that should join keys to their values. Default: "="
128
QueryString.parse = function (qs, sep, eq) {
129
// wouldn't Y.Array(qs.split()).map(pieceParser(eq)).reduce(mergeParams) be prettier?
130
return Y.Array.reduce(
132
qs.split(sep || "&"),
133
pieceParser(eq || "=")
141
* Provides Y.QueryString.unescape method to be able to override default decoding
142
* method. This is important in cases where non-standard delimiters are used, if
143
* the delimiters would not normally be handled properly by the builtin
144
* (en|de)codeURIComponent functions.
145
* Default: replace "+" with " ", and then decodeURIComponent behavior.
146
* @module querystring
147
* @submodule querystring-parse
150
* @param s {String} String to be decoded.
154
QueryString.unescape = function (s) {
155
return decodeURIComponent(s.replace(/\+/g, ' '));
161
}, '3.4.1' ,{requires:['array-extras', 'yui-base']});