Comparing version 0.1.5 to 0.1.6
{ | ||
"author" : "Ruslan Kerimov <rus.kerimov@gmail.com>", | ||
"name" : "susanin", | ||
"version" : "0.1.5", | ||
"version" : "0.1.6", | ||
"repository" : { | ||
@@ -6,0 +6,0 @@ "type" : "git", |
@@ -590,3 +590,3 @@ (function(global) { | ||
Router.prototype.restoreFromBundle = function(bundle) { | ||
Router.prototype.addRoutesFromBundle = function(bundle) { | ||
var i, size; | ||
@@ -593,0 +593,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(e){function p(e,t){if(!(this instanceof p))return new p(e,t);if(t){this._name=t[0],this._defaults=t[1],this._paramsMap=t[2],this._parseRegExpSource=t[3],this._buildFnSource=t[4],this._data=t[5],this._buildFn=new Function("p",this._buildFnSource),this._parseRegExp=new RegExp(this._parseRegExpSource);return}if(!e||typeof e!="object")throw new Error("You must specify options");if(typeof e.name!="string")throw new Error("You must specify the name of the route");this._name=e.name;if(typeof e.pattern!="string")throw new Error("You must specify the pattern of the route");this._pattern=e.pattern,this._conditions=e.conditions&&typeof e.conditions=="object"?e.conditions:{},this._defaults=e.defaults&&typeof e.defaults=="object"?e.defaults:{},this._data=e.data,this._pattern+=a+"?"+o+"query_string"+u+f,this._conditions.query_string=".*",this._parts=this._parsePattern(this._pattern),this._buildParseRegExp()._buildBuildFn()}function d(e){if(!(this instanceof d))return new d(e);this._routes=[],this._routesByName={},e&&this.restoreFromBundle(e)}var t=Object.prototype.hasOwnProperty,n=Object.prototype.toString,r=function(e){return n.call(e)==="[object Array]"},i={parse:function(e,t,n){var r={},i,s,o,u,a,f;arguments.length||(e=location.search.substr(1));if(!e)return r;t||(t="&"),n||(n="="),i=e.split(t);for(a=0,f=i.length;a<f;++a)s=i[a].split(n),o=typeof s[1]!="undefined"?decodeURIComponent(s[1].replace(/\+/g,"%20")):"",u=decodeURIComponent(s[0]),r.hasOwnProperty(u)?Array.isArray(r[u])?r[u].push(o):r[u]=[r[u],o]:r[u]=o;return r},stringify:function(e,t,n){var r="",i,s,o,u,a,f;if(!e)return r;t||(t="&"),n||(n="=");for(f in e)if(e.hasOwnProperty(f)){o=[].concat(e[f]);for(u=0,a=o.length;u<a;++u)s=typeof o[u],s==="object"||s==="undefined"?i="":i=encodeURIComponent(o[u]),r+=t+encodeURIComponent(f)+n+i}return r.substr(t.length)}},s=function(){var e=["/",".","*","+","?","|","(",")","[","]","{","}","\\"],t=new RegExp("(\\"+e.join("|\\")+")","g");return function(e){return e.replace(t,"\\$1")}}(),o="<",u=">",a="(",f=")",l="[a-zA-Z_][\\w\\-]*",c="[\\w\\-]+",h=new RegExp("("+s(o)+l+s(u)+"|"+"[^"+s(o)+s(u)+"]+"+"|"+s(o)+"|"+s(u)+")","g");p.prototype._parsePattern=function(e){var t=[],n="",r,i=0,s,o,u=0,l=!1,c=e.length;while(i<c){r=e.charAt(i++);if(r===a)l?(++u,n+=r):(this._parseParams(n,t),n="",u=0,l=!0);else if(r===f)if(l)if(u===0){n={what:"optional",dependOnParams:[],parts:this._parsePattern(n)},t.push(n);for(s=0,o=n.parts.length;s<o;++s)n.parts[s]&&n.parts[s].what==="param"&&n.dependOnParams.push(n.parts[s].name);n="",l=!1}else--u,n+=r;else n+=r;else n+=r}return this._parseParams(n,t),t},p.prototype._parseParams=function(e,t){var n=e.match(h),r,i,s;if(n)for(r=0,i=n.length;r<i;++r)s=n[r],s.charAt(0)===o&&s.charAt(s.length-1)===u?t.push({what:"param",name:s.substr(1,s.length-2)}):t.push(s)},p.prototype._buildParseRegExp=function(){function t(r){var i="",o,u,a;for(o=0,u=r.length;o<u;++o)a=r[o],typeof a=="string"?i+=s(a):a&&a.what==="param"?(e._paramsMap.push(a.name),i+="("+n(a.name)+")"):a&&a.what==="optional"&&(i+="(?:"+t(a.parts)+")?");return i}function n(t){var n="",i=e._conditions&&e._conditions[t];return i?r(i)?n="(?:"+i.join("|")+")":n=i+"":n=c,n}var e=this;return this._paramsMap=[],this._parseRegExpSource="^"+t(this._parts)+"$",this._parseRegExp=new RegExp(this._parseRegExpSource),this},p.prototype._buildBuildFn=function(){function n(r){var i='""',o,u,a,f,l,c;for(o=0,u=r.length;o<u;++o){l=r[o];if(typeof l=="string")i+='+"'+s(l)+'"';else if(l&&l.what==="param")i+='+(h.call(p,"'+s(l.name)+'")?'+'p["'+s(l.name)+'"]:'+(e._defaults&&t.call(e._defaults,l.name)?'"'+s(e._defaults[l.name])+'"':'""')+")";else if(l&&l.what==="optional"){i+="+((false";for(a=0,f=l.dependOnParams.length;a<f;++a)c=l.dependOnParams[a],i+='||(h.call(p,"'+s(c)+'")'+(e._defaults&&t.call(e._defaults,c)?'&&p["'+s(c)+'"]!=="'+s(e._defaults[c])+'"':"")+")";i+=")?("+n(l.parts)+'):"")'}}return i}var e=this;return this._buildFnSource="var h=({}).hasOwnProperty;return "+n(this._parts)+";",this._buildFn=new Function("p",this._buildFnSource),this},p.prototype.match=function(e){var n=null,r,s,o,u,a;typeof e=="string"?e={path:e}:e||(e={});for(u in e)if(e.hasOwnProperty(u)&&u!=="path")if(!this._data||typeof this._data!="object"||this._data[u]!==e[u])return n;if(typeof e.path=="string"){r=e.path.match(this._parseRegExp);if(r){n={};for(s=1,o=r.length;s<o;++s)typeof r[s]!="undefined"&&r[s]!==""&&(n[this._paramsMap[s-1]]=r[s]);for(u in this._defaults)t.call(this._defaults,u)&&!t.call(n,u)&&(n[u]=this._defaults[u]);a=i.parse(n.query_string);for(u in a)t.call(a,u)&&!t.call(n,u)&&(n[u]=a[u]);delete n.query_string}}else n={};return n},p.prototype.build=function(e){var n={},r={},s,o,u,a,f;for(o in e)if(t.call(e,o)&&e[o]!==null&&typeof e[o]!="undefined"){u=!1;for(a=0,f=this._paramsMap.length;a<f;++a)if(this._paramsMap[a]===o){u=!0;break}u?n[o]=e[o]:r[o]=e[o]}return s=i.stringify(r),s&&(n.query_string=s),this._buildFn(n)},p.prototype.getData=function(){return this._data},p.prototype.getName=function(){return this._name},p.prototype.bundle=function(){return[this._name,this._defaults,this._paramsMap,this._parseRegExpSource,this._buildFnSource,this._data]},d.prototype.addRoute=function(e,t){var n;return n=new p(e,t),this._routes.push(n),this._routesByName[n.getName()]=n,n},d.prototype.find=function(e){var t=[],n,r,i,s=this._routes;for(r=0,i=s.length;r<i;++r)n=s[r].match(e),n!==null&&t.push([s[r],n]);return t},d.prototype.findFirst=function(e){var t,n,r,i=this._routes;for(n=0,r=i.length;n<r;++n){t=i[n].match(e);if(t!==null)return[i[n],t]}return null},d.prototype.getRouteByName=function(e){return this._routesByName[e]||null},d.prototype.bundle=function(){var e=[],t,n,r=this._routes;for(t=0,n=r.length;t<n;++t)e.push(r[t].bundle());return e},d.prototype.restoreFromBundle=function(e){var t,n;for(t=0,n=e.length;t<n;++t)this.addRoute(null,e[t]);return this},d.Route=p,typeof module!="undefined"&&typeof module.exports=="object"?module.exports=d:e.Susanin=d})(this); | ||
(function(e){function p(e,t){if(!(this instanceof p))return new p(e,t);if(t){this._name=t[0],this._defaults=t[1],this._paramsMap=t[2],this._parseRegExpSource=t[3],this._buildFnSource=t[4],this._data=t[5],this._buildFn=new Function("p",this._buildFnSource),this._parseRegExp=new RegExp(this._parseRegExpSource);return}if(!e||typeof e!="object")throw new Error("You must specify options");if(typeof e.name!="string")throw new Error("You must specify the name of the route");this._name=e.name;if(typeof e.pattern!="string")throw new Error("You must specify the pattern of the route");this._pattern=e.pattern,this._conditions=e.conditions&&typeof e.conditions=="object"?e.conditions:{},this._defaults=e.defaults&&typeof e.defaults=="object"?e.defaults:{},this._data=e.data,this._pattern+=a+"?"+o+"query_string"+u+f,this._conditions.query_string=".*",this._parts=this._parsePattern(this._pattern),this._buildParseRegExp()._buildBuildFn()}function d(e){if(!(this instanceof d))return new d(e);this._routes=[],this._routesByName={},e&&this.restoreFromBundle(e)}var t=Object.prototype.hasOwnProperty,n=Object.prototype.toString,r=function(e){return n.call(e)==="[object Array]"},i={parse:function(e,t,n){var r={},i,s,o,u,a,f;arguments.length||(e=location.search.substr(1));if(!e)return r;t||(t="&"),n||(n="="),i=e.split(t);for(a=0,f=i.length;a<f;++a)s=i[a].split(n),o=typeof s[1]!="undefined"?decodeURIComponent(s[1].replace(/\+/g,"%20")):"",u=decodeURIComponent(s[0]),r.hasOwnProperty(u)?Array.isArray(r[u])?r[u].push(o):r[u]=[r[u],o]:r[u]=o;return r},stringify:function(e,t,n){var r="",i,s,o,u,a,f;if(!e)return r;t||(t="&"),n||(n="=");for(f in e)if(e.hasOwnProperty(f)){o=[].concat(e[f]);for(u=0,a=o.length;u<a;++u)s=typeof o[u],s==="object"||s==="undefined"?i="":i=encodeURIComponent(o[u]),r+=t+encodeURIComponent(f)+n+i}return r.substr(t.length)}},s=function(){var e=["/",".","*","+","?","|","(",")","[","]","{","}","\\"],t=new RegExp("(\\"+e.join("|\\")+")","g");return function(e){return e.replace(t,"\\$1")}}(),o="<",u=">",a="(",f=")",l="[a-zA-Z_][\\w\\-]*",c="[\\w\\-]+",h=new RegExp("("+s(o)+l+s(u)+"|"+"[^"+s(o)+s(u)+"]+"+"|"+s(o)+"|"+s(u)+")","g");p.prototype._parsePattern=function(e){var t=[],n="",r,i=0,s,o,u=0,l=!1,c=e.length;while(i<c){r=e.charAt(i++);if(r===a)l?(++u,n+=r):(this._parseParams(n,t),n="",u=0,l=!0);else if(r===f)if(l)if(u===0){n={what:"optional",dependOnParams:[],parts:this._parsePattern(n)},t.push(n);for(s=0,o=n.parts.length;s<o;++s)n.parts[s]&&n.parts[s].what==="param"&&n.dependOnParams.push(n.parts[s].name);n="",l=!1}else--u,n+=r;else n+=r;else n+=r}return this._parseParams(n,t),t},p.prototype._parseParams=function(e,t){var n=e.match(h),r,i,s;if(n)for(r=0,i=n.length;r<i;++r)s=n[r],s.charAt(0)===o&&s.charAt(s.length-1)===u?t.push({what:"param",name:s.substr(1,s.length-2)}):t.push(s)},p.prototype._buildParseRegExp=function(){function t(r){var i="",o,u,a;for(o=0,u=r.length;o<u;++o)a=r[o],typeof a=="string"?i+=s(a):a&&a.what==="param"?(e._paramsMap.push(a.name),i+="("+n(a.name)+")"):a&&a.what==="optional"&&(i+="(?:"+t(a.parts)+")?");return i}function n(t){var n="",i=e._conditions&&e._conditions[t];return i?r(i)?n="(?:"+i.join("|")+")":n=i+"":n=c,n}var e=this;return this._paramsMap=[],this._parseRegExpSource="^"+t(this._parts)+"$",this._parseRegExp=new RegExp(this._parseRegExpSource),this},p.prototype._buildBuildFn=function(){function n(r){var i='""',o,u,a,f,l,c;for(o=0,u=r.length;o<u;++o){l=r[o];if(typeof l=="string")i+='+"'+s(l)+'"';else if(l&&l.what==="param")i+='+(h.call(p,"'+s(l.name)+'")?'+'p["'+s(l.name)+'"]:'+(e._defaults&&t.call(e._defaults,l.name)?'"'+s(e._defaults[l.name])+'"':'""')+")";else if(l&&l.what==="optional"){i+="+((false";for(a=0,f=l.dependOnParams.length;a<f;++a)c=l.dependOnParams[a],i+='||(h.call(p,"'+s(c)+'")'+(e._defaults&&t.call(e._defaults,c)?'&&p["'+s(c)+'"]!=="'+s(e._defaults[c])+'"':"")+")";i+=")?("+n(l.parts)+'):"")'}}return i}var e=this;return this._buildFnSource="var h=({}).hasOwnProperty;return "+n(this._parts)+";",this._buildFn=new Function("p",this._buildFnSource),this},p.prototype.match=function(e){var n=null,r,s,o,u,a;typeof e=="string"?e={path:e}:e||(e={});for(u in e)if(e.hasOwnProperty(u)&&u!=="path")if(!this._data||typeof this._data!="object"||this._data[u]!==e[u])return n;if(typeof e.path=="string"){r=e.path.match(this._parseRegExp);if(r){n={};for(s=1,o=r.length;s<o;++s)typeof r[s]!="undefined"&&r[s]!==""&&(n[this._paramsMap[s-1]]=r[s]);for(u in this._defaults)t.call(this._defaults,u)&&!t.call(n,u)&&(n[u]=this._defaults[u]);a=i.parse(n.query_string);for(u in a)t.call(a,u)&&!t.call(n,u)&&(n[u]=a[u]);delete n.query_string}}else n={};return n},p.prototype.build=function(e){var n={},r={},s,o,u,a,f;for(o in e)if(t.call(e,o)&&e[o]!==null&&typeof e[o]!="undefined"){u=!1;for(a=0,f=this._paramsMap.length;a<f;++a)if(this._paramsMap[a]===o){u=!0;break}u?n[o]=e[o]:r[o]=e[o]}return s=i.stringify(r),s&&(n.query_string=s),this._buildFn(n)},p.prototype.getData=function(){return this._data},p.prototype.getName=function(){return this._name},p.prototype.bundle=function(){return[this._name,this._defaults,this._paramsMap,this._parseRegExpSource,this._buildFnSource,this._data]},d.prototype.addRoute=function(e,t){var n;return n=new p(e,t),this._routes.push(n),this._routesByName[n.getName()]=n,n},d.prototype.find=function(e){var t=[],n,r,i,s=this._routes;for(r=0,i=s.length;r<i;++r)n=s[r].match(e),n!==null&&t.push([s[r],n]);return t},d.prototype.findFirst=function(e){var t,n,r,i=this._routes;for(n=0,r=i.length;n<r;++n){t=i[n].match(e);if(t!==null)return[i[n],t]}return null},d.prototype.getRouteByName=function(e){return this._routesByName[e]||null},d.prototype.bundle=function(){var e=[],t,n,r=this._routes;for(t=0,n=r.length;t<n;++t)e.push(r[t].bundle());return e},d.prototype.addRoutesFromBundle=function(e){var t,n;for(t=0,n=e.length;t<n;++t)this.addRoute(null,e[t]);return this},d.Route=p,typeof module!="undefined"&&typeof module.exports=="object"?module.exports=d:e.Susanin=d})(this); |
53626