Jquery-object.js

Source: Internet
Author: User
Tags compact

/**
* 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.2.3
*
**/
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 = this;
Self.keys = {};

if (a.queryobject) {
Jquery.each (A.get (), function (key, Val) {
Self. SET (Key, Val);
});
} else {
Self.parseNew.apply (self, arguments);
}
return self;
};

Queryobject.prototype = {
Queryobject:true,
Parsenew:function () {
var = this;
Self.keys = {};
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 (/^[+-]?[ 1-9][0-9]*$/.test (val))//Simple int regex
val = parseint (Val, 10);
}

val = (!val && val!== 0)? True:val;

Self. SET (Key, Val);
});
});
return self;
},
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, Val) {
if (val) {
var target = this. GET (key);
if (Is (target, Array)) {
For (tval in target) {
Target[tval] = target[tval].tostring ();
}
var index = $.inarray (val, target);
if (index >= 0) {
Key = Target.splice (index, 1);
key = Key[index];
} else {
Return
}
} else if (val! = target) {
Return
}
}
return this. SET (key, NULL). COMPACT ();
},
Remove:function (Key, Val) {
Return This.copy (). REMOVE (Key, Val);
},
Empty:function () {
var = this;
Jquery.each (Self.keys, function (key, value) {
Delete Self.keys[key];
});
return self;
},
Load:function (URL) {
var hash = Url.replace (/^.*?[ #](.+?) (?:\?. +)? $/, "$");
var search = Url.replace (/^.*?[?) (.+?) (?: #.+)? $/, "$");
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 + "";
str = encodeuricomponent (str);
if ($spaces) str = str.replace (/%20/g, "+");
return 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

Jquery-object.js

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.