UNPKG

waypoint

Version:

Waypoint provides browser and server side routing

4 lines 2.58 kB
// Waypoint: Browser Edition v0.2.8 // Written by Luke Morton, MIT licensed. // https://github.com/DrPheltRight/waypoint !function(a){typeof define=="function"&&define.amd?define(a):this.Waypoint=a()}(function(){function a(b){return a[b].exports}return a["./route"]=new function(){var a=this;((function(){var b;b=function(){function c(a,c,d){c instanceof RegExp?this.regex=c:this.regex=b(c,{}),this.method=a.toUpperCase(),d!=null&&(this.callback=d)}var a,b;return a=function(a,b,c){var d,e,f;c=a;for(e=0,f=b.length;e<f;e++){d=b[e];if(b.hasOwnProperty(d)){c=b[d](a);if(c!==a)break}}return c===a?"([a-zA-Z0-9-]+)":c},b=function(b,c){var d,e,f,g;b.indexOf("*")!==-1&&(b=b.replace(/\*/g,"([_.()!\\ %@&a-zA-Z0-9-]+)"));if(e=b.match(/:([^\/]+)/ig))for(f=0,g=e.length;f<g;f++)d=e[f],b=b.replace(d,a(d,c));return new RegExp("^"+b+"$")},c.prototype.match=function(a,b){var c;return b==null&&(b=a,a=null),a!=null&&this.method!==a.toUpperCase()?!1:(c=this.regex.exec(b),c&&c.length!=null?c.slice(1):!1)},c}(),a.exports=b})).call(this)},a["./router"]=new function(){var b=this;((function(){var c,d,e;c=a("./route"),e=Array.isArray,e||(e=function(a){return Object.prototype.toString.call(a)==="[object Array]"}),d=function(){function b(a){var b,c,d,e,f;if(a){f=["routes","baseUri","notFound","multi"];for(d=0,e=f.length;d<e;d++)b=f[d],a[b]!=null&&(this[b]=a[b]);a.routeMap!=null&&(c=a.routeMap)}this.routes||(this.routes=[]),this.baseUri||(this.baseUri=""),this.multi||(this.multi=!1),c!=null&&this.routeMap(c)}var a;return b.prototype.route=function(a,b,d){var e;return typeof a=="object"?e=a:e=new c(a,b,d),this.routes.push(e),this},b.prototype.get=function(a,b){return this.route("GET",a,b)},b.prototype.post=function(a,b){return this.route("POST",a,b)},a=function(a){var b,c,d;return b=a.match(/^(GET|POST) (.+)/),b&&b.length!=null?(d=b.slice(1,3).reverse(),a=d[0],c=d[1],c||(c="GET"),[a,c]):[a||"","GET"]},b.prototype.routeMap=function(b,c){var d,f,g,h;c==null&&(c=this.baseUri);for(g in b){d=b[g],h=a(g),g=h[0],f=h[1],g=c+g;if(typeof d=="function"||e(d))this.route(f,g,d);else if(typeof d=="object")this.routeMap(d,g);else throw"Map must be string array or object"}return this},b.prototype.dispatch=function(a,b,c){var d,f,g,h,i,j,k,l,m,n;c==null&&(c={}),g=!1,n=this.routes;for(j=0,l=n.length;j<l;j++){i=n[j];if(!(h=i.match(a,b)))continue;g=!0,e(i.callback)?f=i.callback:f=[i.callback];for(k=0,m=f.length;k<m;k++)d=f[k],d.apply(c,h);if(!this.multi)break}return this.notFound!=null&&g===!1&&this.notFound.call(c),g},b}(),b.exports=d})).call(this)},{Route:a("./route"),Router:a("./router")}})