UNPKG

5.98 kBJavaScriptView Raw
1/*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */
2!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.UniversalRouter=t()}(this,function(){"use strict";function e(e,t){for(var r,n=[],o=0,u=0,l="",s=t&&t.delimiter||"/";null!=(r=U.exec(e));){var c=r[0],p=r[1],f=r.index;if(l+=e.slice(u,f),u=f+c.length,p)l+=p[1];else{var h=e[u],v=r[2],d=r[3],g=r[4],y=r[5],m=r[6],x=r[7];l&&(n.push(l),l="");var b=null!=v&&null!=h&&h!==v,w="+"===m||"*"===m,E="?"===m||"*"===m,R=r[2]||s,j=g||y;n.push({name:d||o++,prefix:v||"",delimiter:R,optional:E,repeat:w,partial:b,asterisk:!!x,pattern:j?a(j):x?".*":"[^"+i(R)+"]+?"})}}return u<e.length&&(l+=e.substr(u)),l&&n.push(l),n}function t(t,r){return o(e(t,r))}function r(e){return encodeURI(e).replace(/[\/?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function n(e){return encodeURI(e).replace(/[?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function o(e){for(var t=new Array(e.length),o=0;o<e.length;o++)"object"==typeof e[o]&&(t[o]=new RegExp("^(?:"+e[o].pattern+")$"));return function(o,i){for(var a="",u=o||{},l=i||{},s=l.pretty?r:encodeURIComponent,c=0;c<e.length;c++){var p=e[c];if("string"!=typeof p){var f,h=u[p.name];if(null==h){if(p.optional){p.partial&&(a+=p.prefix);continue}throw new TypeError('Expected "'+p.name+'" to be defined')}if(b(h)){if(!p.repeat)throw new TypeError('Expected "'+p.name+'" to not repeat, but received `'+JSON.stringify(h)+"`");if(0===h.length){if(p.optional)continue;throw new TypeError('Expected "'+p.name+'" to not be empty')}for(var v=0;v<h.length;v++){if(f=s(h[v]),!t[c].test(f))throw new TypeError('Expected all "'+p.name+'" to match "'+p.pattern+'", but received `'+JSON.stringify(f)+"`");a+=(0===v?p.prefix:p.delimiter)+f}}else{if(f=p.asterisk?n(h):s(h),!t[c].test(f))throw new TypeError('Expected "'+p.name+'" to match "'+p.pattern+'", but received "'+f+'"');a+=p.prefix+f}}else a+=p}return a}}function i(e){return e.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function a(e){return e.replace(/([=!:$\/()])/g,"\\$1")}function u(e,t){return e.keys=t,e}function l(e){return e.sensitive?"":"i"}function s(e,t){var r=e.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)t.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,asterisk:!1,pattern:null});return u(e,t)}function c(e,t,r){for(var n=[],o=0;o<e.length;o++)n.push(h(e[o],t,r).source);return u(new RegExp("(?:"+n.join("|")+")",l(r)),t)}function p(t,r,n){return f(e(t,n),r,n)}function f(e,t,r){b(t)||(r=t||r,t=[]),r=r||{};for(var n=r.strict,o=!1!==r.end,a="",s=0;s<e.length;s++){var c=e[s];if("string"==typeof c)a+=i(c);else{var p=i(c.prefix),f="(?:"+c.pattern+")";t.push(c),c.repeat&&(f+="(?:"+p+f+")*"),f=c.optional?c.partial?p+"("+f+")?":"(?:"+p+"("+f+"))?":p+"("+f+")",a+=f}}var h=i(r.delimiter||"/"),v=a.slice(-h.length)===h;return n||(a=(v?a.slice(0,-h.length):a)+"(?:"+h+"(?=$))?"),a+=o?"$":n&&v?"":"(?="+h+"|$)",u(new RegExp("^"+a,l(r)),t)}function h(e,t,r){return b(t)||(r=t||r,t=[]),r=r||{},e instanceof RegExp?s(e,t):b(e)?c(e,t,r):p(e,t,r)}function v(e){try{return decodeURIComponent(e)}catch(t){return e}}function d(e,t,r,n){var o=e+"|"+r,i=O.get(o);if(!i){var a=[];i={pattern:w(e,a,{end:r}),keys:a},O.set(o,i)}var u=i.pattern.exec(t);if(!u)return null;var l=u[0],s=Object.create(null);n&&Object.assign(s,n);for(var c=1;c<u.length;c+=1)s[i.keys[c-1].name]=u[c]&&v(u[c]);return{path:""===l?"/":l,keys:i.keys.slice(),params:s}}function g(e,t,r,n){var o=void 0,i=void 0,a=0;return{next:function(){if(!o&&(o=d(e.path,r,!e.children,n)))return{done:!1,value:{route:e,baseUrl:t,path:o.path,keys:o.keys,params:o.params}};if(o&&e.children)for(;a<e.children.length;){if(!i){var u=r.substr(o.path.length),l=e.children[a];l.parent=e,i=g(l,t+("/"===o.path?"":o.path),"/"===u.charAt(0)?u:"/"+u,o.params)}var s=i.next();if(!s.done)return{done:!1,value:s.value};i=null,a+=1}return{done:!0}}}}function y(e,t){return"function"==typeof e.route.action?e.route.action(e,t):null}function m(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function x(e,t){for(var r=t;r;)if((r=r.parent)===e)return!0;return!1}var b=Array.isArray||function(e){return"[object Array]"==Object.prototype.toString.call(e)},w=h,E=e,R=t,j=o,k=f,U=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g");w.parse=E,w.compile=R,w.tokensToFunction=j,w.tokensToRegExp=k;var O=new Map,T=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),A=function(){function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(m(this,e),Object(t)!==t)throw new TypeError("Invalid routes");this.baseUrl=r.baseUrl||"",this.resolveRoute=r.resolveRoute||y,this.context=Object.assign({router:this},r.context),this.root=Array.isArray(t)?{path:"/",children:t,parent:null}:t,this.root.parent=null}return T(e,[{key:"resolve",value:function(e){function t(e){var u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:i.value.route;return i=a||n.next(),a=null,e||!i.done&&x(u,i.value.route)?i.done?Promise.reject(Object.assign(new Error("Page not found"),{context:r,status:404,statusCode:404})):Promise.resolve(o(Object.assign({},r,i.value),i.value.params)).then(function(r){return null!==r&&void 0!==r?r:t(e,u)}):(a=i,Promise.resolve(null))}var r=Object.assign({},this.context,"string"==typeof e?{path:e}:e),n=g(this.root,this.baseUrl,r.path.substr(this.baseUrl.length)),o=this.resolveRoute,i=null,a=null;return r.url=r.path,r.next=t,t(!0,this.root)}}]),e}();return A.pathToRegexp=w,A.matchPath=d,A.matchRoute=g,A.resolveRoute=y,A});
3//# sourceMappingURL=universal-router.min.js.map