universal-router
Version:
Isomorphic router for JavaScript web applications
4 lines (3 loc) • 5.93 kB
JavaScript
/*! Universal Router | MIT License | https://www.kriasoft.com/universal-router/ */
!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";var e=h,t=l,r=function(e,t){return s(l(e,t))},n=s,o=c,a="/",i="./",u=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g");function l(e,t){for(var r,n,o=[],l=0,s=0,f="",c=t&&t.delimiter||a,h=t&&t.delimiters||i,v=!1;null!==(r=u.exec(e));){var d=r[0],g=r[1],m=r.index;if(f+=e.slice(s,m),s=m+d.length,g)f+=g[1],v=!0;else{var y="",x=e[s],b=r[2],w=r[3],E=r[4],R=r[5];if(!v&&f.length){var j=f.length-1;h.indexOf(f[j])>-1&&(y=f[j],f=f.slice(0,j))}f&&(o.push(f),f="",v=!1);var k=""!==y&&void 0!==x&&x!==y,O="+"===R||"*"===R,T="?"===R||"*"===R,A=y||c,U=w||E;o.push({name:b||l++,prefix:y,delimiter:A,optional:T,repeat:O,partial:k,pattern:U?(n=U,n.replace(/([=!:$/()])/g,"\\$1")):"[^"+p(A)+"]+?"})}}return(f||s<e.length)&&o.push(f+e.substr(s)),o}function s(e){for(var t=new Array(e.length),r=0;r<e.length;r++)"object"==typeof e[r]&&(t[r]=new RegExp("^(?:"+e[r].pattern+")$"));return function(r,n){for(var o="",a=n&&n.encode||encodeURIComponent,i=0;i<e.length;i++){var u=e[i];if("string"!=typeof u){var l,s=r?r[u.name]:void 0;if(Array.isArray(s)){if(!u.repeat)throw new TypeError('Expected "'+u.name+'" to not repeat, but got array');if(0===s.length){if(u.optional)continue;throw new TypeError('Expected "'+u.name+'" to not be empty')}for(var p=0;p<s.length;p++){if(l=a(s[p]),!t[i].test(l))throw new TypeError('Expected all "'+u.name+'" to match "'+u.pattern+'"');o+=(0===p?u.prefix:u.delimiter)+l}}else if("string"!=typeof s&&"number"!=typeof s&&"boolean"!=typeof s){if(!u.optional)throw new TypeError('Expected "'+u.name+'" to be '+(u.repeat?"an array":"a string"));u.partial&&(o+=u.prefix)}else{if(l=a(String(s)),!t[i].test(l))throw new TypeError('Expected "'+u.name+'" to match "'+u.pattern+'", but got "'+l+'"');o+=u.prefix+l}}else o+=u}return o}}function p(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function f(e){return e&&e.sensitive?"":"i"}function c(e,t,r){for(var n=(r=r||{}).strict,o=!1!==r.end,u=p(r.delimiter||a),l=r.delimiters||i,s=[].concat(r.endsWith||[]).map(p).concat("$").join("|"),c="",h=!1,v=0;v<e.length;v++){var d=e[v];if("string"==typeof d)c+=p(d),h=v===e.length-1&&l.indexOf(d[d.length-1])>-1;else{var g=p(d.prefix),m=d.repeat?"(?:"+d.pattern+")(?:"+g+"(?:"+d.pattern+"))*":d.pattern;t&&t.push(d),d.optional?d.partial?c+=g+"("+m+")?":c+="(?:"+g+"("+m+"))?":c+=g+"("+m+")"}}return o?(n||(c+="(?:"+u+")?"),c+="$"===s?"$":"(?="+s+")"):(n||(c+="(?:"+u+"(?="+s+"))?"),h||(c+="(?="+u+"|"+s+")")),new RegExp("^"+c,f(r))}function h(e,t,r){return e instanceof RegExp?function(e,t){if(!t)return e;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,pattern:null});return e}(e,t):Array.isArray(e)?function(e,t,r){for(var n=[],o=0;o<e.length;o++)n.push(h(e[o],t,r).source);return new RegExp("(?:"+n.join("|")+")",f(r))}(e,t,r):(n=t,c(l(e,o=r),n,o));var n,o}e.parse=t,e.compile=r,e.tokensToFunction=n,e.tokensToRegExp=o;var v=Object.prototype.hasOwnProperty,d=new Map;function g(e){try{return decodeURIComponent(e)}catch(t){return e}}function m(t,r,n,o){var a=!t.children,i=(t.path||"")+"|"+a,u=d.get(i);if(!u){var l=[];u={keys:l,pattern:e(t.path||"",l,{end:a})},d.set(i,u)}var s=u.pattern.exec(r);if(!s)return null;for(var p=s[0],f=Object.assign({},o),c=1;c<s.length;c+=1){var h=u.keys[c-1],m=h.name,y=s[c];void 0===y&&v.call(f,m)||(h.repeat?f[m]=y?y.split(h.delimiter).map(g):[]:f[m]=y?g(y):y)}return{path:a||"/"!==p.charAt(p.length-1)?p:p.substr(1),keys:n.concat(u.keys),params:f}}function y(e,t,r,n,o){var a=void 0,i=void 0,u=0;return{next:function(l){if(e===l)return{done:!0};if(!a&&(a=m(e,r,n,o)))return{done:!1,value:{route:e,baseUrl:t,path:a.path,keys:a.keys,params:a.params}};if(a&&e.children)for(;u<e.children.length;){if(!i){var s=e.children[u];s.parent=e,i=y(s,t+a.path,r.substr(a.path.length),a.keys,a.params)}var p=i.next(l);if(!p.done)return{done:!1,value:p.value};i=null,u+=1}return{done:!0}}}}function x(e,t){if("function"==typeof e.route.action)return e.route.action(e,t)}var b=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}}();var w=function(){function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),Object(t)!==t)throw new TypeError("Invalid routes");this.baseUrl=r.baseUrl||"",this.resolveRoute=r.resolveRoute||x,this.context=Object.assign({router:this},r.context),this.root=Array.isArray(t)?{path:"",children:t,parent:null}:t,this.root.parent=null}return b(e,[{key:"resolve",value:function(e){var t=Object.assign({},this.context,"string"==typeof e?{pathname:e}:e),r=y(this.root,this.baseUrl,t.pathname.substr(this.baseUrl.length),[],null),n=this.resolveRoute,o=null,a=null;function i(e){var u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o.value.route,l=null===arguments[2]&&o.value.route;return o=a||r.next(l),a=null,e||!o.done&&function(e,t){for(var r=t;r;)if((r=r.parent)===e)return!0;return!1}(u,o.value.route)?o.done?Promise.reject(Object.assign(new Error("Page not found"),{context:t,status:404,statusCode:404})):Promise.resolve(n(Object.assign({},t,o.value),o.value.params)).then(function(t){return null!==t&&void 0!==t?t:i(e,u,t)}):(a=o,Promise.resolve(null))}return t.next=i,i(!0,this.root)}}]),e}();return w.pathToRegexp=e,w.matchPath=m,w.matchRoute=y,w.resolveRoute=x,w});
//# sourceMappingURL=universal-router.min.js.map