2
* jQuery.query - Query String Modification and Creation for jQuery
3
* Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
4
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
7
* @author Blair Mitchelmore
11
new function(settings) {
13
var $separator = settings.separator || '&';
14
var $spaces = settings.spaces === false ? false : true;
15
var $suffix = settings.suffix === false ? '' : '[]';
16
var $prefix = settings.prefix === false ? false : true;
17
var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
18
var $numbers = settings.numbers === false ? false : true;
20
jQuery.query = new function() {
21
var is = function(o, t) {
22
return o != undefined && o !== null && (!!t ? o.constructor == t : true);
24
var parse = function(path) {
25
var m, rx = /\[([^[]*)\]/g, match = /^([^[]+)(\[.*\])?$/.exec(path), base = match[1], tokens = [];
26
while (m = rx.exec(match[2])) tokens.push(m[1]);
27
return [base, tokens];
29
var set = function(target, tokens, value) {
30
var o, token = tokens.shift();
31
if (typeof target != 'object') target = null;
33
if (!target) target = [];
34
if (is(target, Array)) {
35
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
36
} else if (is(target, Object)) {
38
while (target[i++] != null);
39
target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
42
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
44
} else if (token && token.match(/^\s*[0-9]+\s*$/)) {
45
var index = parseInt(token, 10);
46
if (!target) target = [];
47
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
49
var index = token.replace(/^\s*|\s*$/g, "");
50
if (!target) target = {};
51
if (is(target, Array)) {
53
for (var i = 0; i < target.length; ++i) {
58
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
65
var queryObject = function(a) {
70
jQuery.each(a.get(), function(key, val) {
74
jQuery.each(arguments, function() {
76
q = q.replace(/^[?#]/,''); // remove any leading ? || #
77
q = q.replace(/[;&]$/,''); // remove any trailing & || ;
78
if ($spaces) q = q.replace(/[+]/g,' '); // replace +'s with spaces
80
jQuery.each(q.split(/[&;]/), function(){
81
var key = decodeURIComponent(this.split('=')[0] || "");
82
var val = decodeURIComponent(this.split('=')[1] || "");
87
if (/^[+-]?[0-9]+\.[0-9]*$/.test(val)) // simple float regex
88
val = parseFloat(val);
89
else if (/^[+-]?[0-9]+$/.test(val)) // simple int regex
90
val = parseInt(val, 10);
93
val = (!val && val !== 0) ? true : val;
95
if (val !== false && val !== true && typeof val != 'number')
105
queryObject.prototype = {
107
has: function(key, type) {
108
var value = this.get(key);
109
return is(value, type);
112
if (!is(key)) return this.keys;
113
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
114
var target = this.keys[base];
115
while (target != null && tokens.length != 0) {
116
target = target[tokens.shift()];
118
return typeof target == 'number' ? target : target || "";
121
var target = this.GET(key);
122
if (is(target, Object))
123
return jQuery.extend(true, {}, target);
124
else if (is(target, Array))
125
return target.slice(0);
128
SET: function(key, val) {
129
var value = !is(val) ? null : val;
130
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
131
var target = this.keys[base];
132
this.keys[base] = set(target, tokens.slice(0), value);
135
set: function(key, val) {
136
return this.copy().SET(key, val);
138
REMOVE: function(key) {
139
return this.SET(key, null).COMPACT();
141
remove: function(key) {
142
return this.copy().REMOVE(key);
146
jQuery.each(self.keys, function(key, value) {
147
delete self.keys[key];
151
load: function(url) {
152
var hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
153
var search = url.replace(/^.*?[?](.+?)(?:#.+)?$/, "$1");
154
return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
157
return this.copy().EMPTY();
160
return new queryObject(this);
162
COMPACT: function() {
163
function build(orig) {
164
var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig;
165
if (typeof orig == 'object') {
166
function add(o, key, value) {
172
jQuery.each(orig, function(key, value) {
173
if (!is(value)) return true;
174
add(obj, key, build(value));
179
this.keys = build(this.keys);
182
compact: function() {
183
return this.copy().COMPACT();
185
toString: function() {
186
var i = 0, queryString = [], chunks = [], self = this;
187
var encode = function(str) {
189
if ($spaces) str = str.replace(/ /g, "+");
190
return encodeURIComponent(str);
192
var addFields = function(arr, key, value) {
193
if (!is(value) || value === false) return;
194
var o = [encode(key)];
195
if (value !== true) {
197
o.push(encode(value));
199
arr.push(o.join(""));
201
var build = function(obj, base) {
202
var newKey = function(key) {
203
return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
205
jQuery.each(obj, function(key, value) {
206
if (typeof value == 'object')
207
build(value, newKey(key));
209
addFields(chunks, newKey(key), value);
215
if (chunks.length > 0) queryString.push($hash);
216
queryString.push(chunks.join($separator));
218
return queryString.join("");
222
return new queryObject(location.search, location.hash);
224
}(jQuery.query || {}); // Pass in jQuery.query as settings object