forked from deleteme/params.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.min.js
2 lines (2 loc) · 2.14 KB
/
params.min.js
1
2
// params.js 0.6.0
(function(){var a;return a=typeof exports!="undefined"&&exports!==null?exports:this,a.Params=function(){function b(c){if(this._location=c||a.location,this._search=this._location.search,this._prefix=this._location.protocol+"//"+this._location.host+this._location.pathname,this._params={},this._pairs=[],!this.validate(this._search))throw"Initializing Params with invalid location.search.";this._params=b.parse(this._search),this._buildPairs()}return b.prototype._set=function(a,b){return this._params[a]=b,this._buildPairs(),b},b.prototype._buildPairs=function(){var a,b;return this._pairs=function(){var c;c=[];for(b in this._params)Object.prototype.toString.call(this._params[b])==="[object Array]"?c.push(function(){var c;c=[];for(a in this._params[b])c.push(""+b+"="+this._params[b][a]);return c}.call(this).join("&")):c.push(""+b+"="+this._params[b]);return c}.call(this)},b.prototype.href=function(){return this._prefix+this.search()},b.prototype.search=function(){return this._pairs.length?"?"+this._pairs.join("&"):""},b.prototype.set=function(){var a,b;if(a=arguments[0],typeof a=="string"||typeof a=="number")this._set(a+"",arguments[1]);else{if(typeof a!="object")throw"Unexpected data type for: "+a+". Should be a string, number, or object";for(b in a)this._set(b,a[b])}return this},b.prototype.unset=function(a){var b;return b=this._params[a],delete this._params[a],this._buildPairs(),this},b.prototype._validations=[function(a){return typeof a=="string"},function(a){return a.length?a.match(/^\?/):!0},function(a){return!a.match(/&$/)}],b.prototype.validate=function(a){var b,c,d,e,f;for(b=!0,f=this._validations,d=0,e=f.length;e>d;d++)if(c=f[d],b=c(a),!b)break;return b},b.prototype.get=function(a){return this._params[a]},b.prototype.object=function(){var a,b;b={};for(a in this._params)b[a]=this._params[a];return b},b}(),a.Params.parse=function(a){var b,c,d,e,f,g,h;if(e={},d=[],a.length>1)for(d=a.slice(1+a.indexOf("?"),a.length).split("&"),g=0,h=d.length;h>g;g++)c=d[g],c=c.split("="),b=c[0],f=c[1],b in e?(Object.prototype.toString.call(e[b])!=="[object Array]"&&(e[b]=[e[b]]),e[b].push(decodeURIComponent(f))):e[b]=decodeURIComponent(f);return e}})()