-
Notifications
You must be signed in to change notification settings - Fork 29
/
minrouter.min.js
1 lines (1 loc) · 3.69 KB
/
minrouter.min.js
1
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Router=t():e.Router=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.i=function(e){return e},t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function n(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,r){function n(o,u){try{var c=t[o](u),a=c.value}catch(e){return void r(e)}if(!c.done)return Promise.resolve(a).then(function(e){n("next",e)},function(e){n("throw",e)});e(a)}return n("next")})}}function o(e){var t=[];return e=e.replace(p[0],"\\$&").replace(p[1],"(?:$1)?").replace(p[2],function(e,r){return e&&t.push(e.replace(":","")),r?e:"([^/?]+)"}).replace(p[3],function(e,r){return e&&t.push(e.replace("*","")),"([^?]*?)"}),{regexp:new RegExp("^"+e+"(?:\\?([\\s\\S]*))?$"),matchs:t}}function u(e,t){for(var r=e.exec(t).slice(1),n=[],o=0;o<r.length;o++)n.push(decodeURIComponent(r[o])||null);return n}function c(){var e=location.search,t=/(\w+)=([^\?|^\&]+)/gi,r={};return e.replace(t,function(e,t,n){r[t]=n}),r}function a(){l.req.url!==location.href&&(l.req.url=location.href,l.req.path=location.pathname,l.req.query=c(),s())}var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},s=function(){var e=n(regeneratorRuntime.mark(function e(){var t,r,n,c,a;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:t=!1,r=0;case 2:if(!(r<l.routes.length)){e.next=15;break}if(n=o(l.routes[r].path),n.regexp.test(l.req.path)){e.next=6;break}return e.abrupt("continue",12);case 6:for(t=!0,c=u(n.regexp,l.req.path),l.req.params=l.req.params||{},a=0;a<n.matchs.length;a++)l.req.params[n.matchs[a]]=c[a];return e.next=12,l.routes[r].fn.call(l,l.req,l.res,l.next);case 12:r++,e.next=2;break;case 15:if(t||"function"!=typeof l.next){e.next=18;break}return e.next=18,l.next();case 18:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}(),f=function(){var e=n(regeneratorRuntime.mark(function e(t,r,n){var o;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:"object"===("undefined"==typeof document?"undefined":i(document))?(l.env="browser",l.req={query:{}},l.res={},window.addEventListener("popstate",a,!1)):n?(l.req=t,l.req.url=l.req.originalUrl,l.res=r,l.next=n,l.env="express"):(l.ctx=t,l.req=l.ctx.request,l.req.path=l.req.url.replace(/\?.*/g,""),l.res=l.ctx.response,l.next=r,l.env="koa");for(o in l.resMethods)l.res[o]=l.resMethods[o].bind(l);if("browser"!==l.env){e.next=6;break}a(),e.next=8;break;case 6:return e.next=8,s();case 8:case"end":return e.stop()}},e,this)}));return function(t,r,n){return e.apply(this,arguments)}}(),p=[/[\-{}\[\]+?.,\\\^$|#\s]/g,/\((.*?)\)/g,/(\(\?)?:\w+/g,/\*\w+/g],l={routes:[],resMethods:{}};f.get=function(e,t){l.routes.push({path:e,fn:t})},f.addResMethod=function(e,t){l.resMethods[e]=t},f.go=function(e,t){t?history.replaceState({path:e},null,e):history.pushState({path:e},null,e),a()},f.back=function(){history.back()},f.proxyLinks=function(e){for(var t=0;t<e.length;t++)e[t].addEventListener("click",function(e){f.go(e.target.href),e.preventDefault()})},e.exports=f}])});