jquery.params.js源码分享
jquery.params.js 源码
2018年10月09日 09:31:10 zerone羽 数:2 标签: javascript jquery 个人分类: 脚本 /*** jquery.query - query string modification and creation for jquery
* written by blair mitchelmore (blair dot mitchelmore at gmail dot com)
* licensed under the wtfpl (http://sam.zoy.org/wtfpl/).
* date: 2009/8/13
*
* @author blair mitchelmore
* @version 2.1.7
*
**/
new function (settings) {
// various settings
var $separator = settings.separator || '&';
var $spaces = settings.spaces === false false : true;
var $suffix = settings.suffix === false '' : '[]';
var $prefix = settings.prefix === false false : true;
var $hash = $prefix settings.hash === true "#" : "" : "";
var $numbers = settings.numbers === false false : true;
jquery.query = new function () {
var is = function (o, t) {
return o != undefined && o !== null && (!!t o.constructor == t : true);
};
var parse = function (path) {
var m, rx = /\[([^[]*)\]/g, match = /^([^[]+)(\[.*\])$/.exec(path), base = match[1], tokens = [];
while (m = rx.exec(match[2])) tokens.push(m[1]);
return [base, tokens];
};
var set = function (target, tokens, value) {
var o, token = tokens.shift();
if (typeof target != 'object') target = null;
if (token === "") {
if (!target) target = [];
if (is(target, array)) {
target.push(tokens.length == 0 value : set(null, tokens.slice(0), value));
} else if (is(target, object)) {
var i = 0;
while (target[i++] != null);
target[--i] = tokens.length == 0 value : set(target[i], tokens.slice(0), value);
} else {
target = [];
target.push(tokens.length == 0 value : set(null, tokens.slice(0), value));
}
} else if (token && token.match(/^\s*[0-9]+\s*$/)) {
var index = parseint(token, 10);
if (!target) target = [];
target[index] = tokens.length == 0 value : set(target[index], tokens.slice(0), value);
} else if (token) {
var index = token.replace(/^\s*|\s*$/g, "");
if (!target) target = {};
if (is(target, array)) {
var temp = {};
for (var i = 0; i < target.length; ++i) {
temp[i] = target[i];
}
target = temp;
}
target[index] = tokens.length == 0 value : set(target[index], tokens.slice(0), value);
} else {
return value;
}
return target;
};
var queryobject = function (a) {
var self = this;
self.keys = {};
if (a.queryobject) {
jquery.each(a.get(), function (key, val) {
self.set(key, val);
});
} else {
jquery.each(arguments, function () {
var q = "" + this;
q = q.replace(/^[#]/, ''); // remove any leading || #
q = q.replace(/[;&]$/, ''); // remove any trailing & || ;
if ($spaces) q = q.replace(/[+]/g, ' '); // replace +'s with spaces
jquery.each(q.split(/[&;]/), function () {
var key = decodeuricomponent(this.split('=')[0] || "");
var val = decodeuricomponent(this.split('=')[1] || "");
if (!key) return;
if ($numbers) {
if (/^[+-][0-9]+\.[0-9]*$/.test(val)) // simple float regex
val = parsefloat(val);
else if (/^[+-][0-9]+$/.test(val)) // simple int regex
val = parseint(val, 10);
}
val = (!val && val !== 0) true : val;
if (val !== false && val !== true && typeof val != 'number')
val = val;
self.set(key, val);
});
});
}
return self;
};
queryobject.prototype = {
queryobject: true,
has: function (key, type) {
var value = this.get(key);
return is(value, type);
},
get: function (key) {
if (!is(key)) return this.keys;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
while (target != null && tokens.length != 0) {
target = target[tokens.shift()];
}
return typeof target == 'number' target : target || "";
},
get: function (key) {
var target = this.get(key);
if (is(target, object))
return jquery.extend(true, {}, target);
else if (is(target, array))
return target.slice(0);
return target;
},
set: function (key, val) {
var value = !is(val) null : val;
var parsed = parse(key), base = parsed[0], tokens = parsed[1];
var target = this.keys[base];
this.keys[base] = set(target, tokens.slice(0), value);
return this;
},
set: function (key, val) {
return this.copy().set(key, val);
},
remove: function (key) {
return this.set(key, null).compact();
},
remove: function (key) {
return this.copy().remove(key);
},
empty: function () {
var self = this;
jquery.each(self.keys, function (key, value) {
delete self.keys[key];
});
return self;
},
load: function (url) {
var hash = url.replace(/^.*[#](.+)(:\.+)$/, "$1");
var search = url.replace(/^.*[](.+)(:#.+)$/, "$1");
return new queryobject(url.length == search.length '' : search, url.length == hash.length '' : hash);
},
empty: function () {
return this.copy().empty();
},
copy: function () {
return new queryobject(this);
},
compact: function () {
function build(orig) {
var obj = typeof orig == "object" is(orig, array) [] : {} : orig;
if (typeof orig == 'object') {
function add(o, key, value) {
if (is(o, array))
o.push(value);
else
o[key] = value;
}
jquery.each(orig, function (key, value) {
if (!is(value)) return true;
add(obj, key, build(value));
});
}
return obj;
}
this.keys = build(this.keys);
return this;
},
compact: function () {
return this.copy().compact();
},
tostring: function () {
var i = 0, querystring = [], chunks = [], self = this;
var encode = function (str) {
str = str + "";
if ($spaces) str = str.replace(/ /g, "+");
return encodeuricomponent(str);
};
var addfields = function (arr, key, value) {
if (!is(value) || value === false) return;
var o = [encode(key)];
if (value !== true) {
o.push("=");
o.push(encode(value));
}
arr.push(o.join(""));
};
var build = function (obj, base) {
var newkey = function (key) {
return !base || base == "" [key].join("") : [base, "[", key, "]"].join("");
};
jquery.each(obj, function (key, value) {
if (typeof value == 'object')
build(value, newkey(key));
else
addfields(chunks, newkey(key), value);
});
};
build(this.keys);
if (chunks.length > 0) querystring.push($hash);
querystring.push(chunks.join($separator));
return querystring.join("");
}
};
return new queryobject(location.search, location.hash);
};
} (jquery.query || {}); // pass in jquery.query as settings object
上一篇: 华夏文化是怎么融合的?乾隆在崇尚汉文化的同时亦在抵制汉文化!
下一篇: 外边冷