UNPKG

@curi/router

Version:

A JavaScript router that doesn't care how you render

2 lines (1 loc) 8.49 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).Curi={})}(this,function(e){"use strict";function A(e){return"externalURL"in e}function T(e,t,n,r,i){var o=n||{},a=o.resolved,u=void 0===a?null:a,c=o.error,l=void 0===c?null:c,p={data:void 0,body:void 0,meta:void 0};for(var f in t)p[f]=t[f];if(!e.methods.respond)return p;var s=e.methods.respond({resolved:u,error:l,match:t,external:i});return s&&(p.meta=s.meta,p.body=s.body,p.data=s.data,s.redirect&&(p.redirect=function(e,t){if(A(e))return e;var n=e.name,r=e.params,i=e.query,o=e.hash;return{name:n,params:r,query:i,hash:o,state:e.state,url:"url"in e?e.url:t.url({name:n,params:r,query:i,hash:o})}}(s.redirect,r))),p}function t(e,t){return a(o(e,t))}var w=c,n=o,r=a,i=u,$="/",j="./",q=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g");function o(e,t){for(var n,r,i=[],o=0,a=0,u="",c=t&&t.delimiter||$,l=t&&t.delimiters||j,p=!1;null!==(n=q.exec(e));){var f=n[0],s=n[1],d=n.index;if(u+=e.slice(a,d),a=d+f.length,s)u+=s[1],p=!0;else{var h="",v=e[a],m=n[2],g=n[3],y=n[4],x=n[5];if(!p&&u.length){var b=u.length-1;-1<l.indexOf(u[b])&&(h=u[b],u=u.slice(0,b))}u&&(i.push(u),u="",p=!1);var w=""!==h&&void 0!==v&&v!==h,E="+"===x||"*"===x,R="?"===x||"*"===x,k=h||c,A=g||y;i.push({name:m||o++,prefix:h,delimiter:k,optional:R,repeat:E,partial:w,pattern:A?(r=A,r.replace(/([=!:$/()])/g,"\\$1")):"[^"+C(k)+"]+?"})}}return(u||a<e.length)&&i.push(u+e.substr(a)),i}function a(l){for(var p=new Array(l.length),e=0;e<l.length;e++)"object"==typeof l[e]&&(p[e]=new RegExp("^(?:"+l[e].pattern+")$"));return function(e,t){for(var n="",r=t&&t.encode||encodeURIComponent,i=0;i<l.length;i++){var o=l[i];if("string"!=typeof o){var a,u=e?e[o.name]:void 0;if(Array.isArray(u)){if(!o.repeat)throw new TypeError('Expected "'+o.name+'" to not repeat, but got array');if(0===u.length){if(o.optional)continue;throw new TypeError('Expected "'+o.name+'" to not be empty')}for(var c=0;c<u.length;c++){if(a=r(u[c],o),!p[i].test(a))throw new TypeError('Expected all "'+o.name+'" to match "'+o.pattern+'"');n+=(0===c?o.prefix:o.delimiter)+a}}else if("string"!=typeof u&&"number"!=typeof u&&"boolean"!=typeof u){if(!o.optional)throw new TypeError('Expected "'+o.name+'" to be '+(o.repeat?"an array":"a string"));o.partial&&(n+=o.prefix)}else{if(a=r(String(u),o),!p[i].test(a))throw new TypeError('Expected "'+o.name+'" to match "'+o.pattern+'", but got "'+a+'"');n+=o.prefix+a}}else n+=o}return n}}function C(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function h(e){return e&&e.sensitive?"":"i"}function u(e,t,n){for(var r=(n=n||{}).strict,i=!1!==n.start,o=!1!==n.end,a=C(n.delimiter||$),u=n.delimiters||j,c=[].concat(n.endsWith||[]).map(C).concat("$").join("|"),l=i?"^":"",p=0===e.length,f=0;f<e.length;f++){var s=e[f];if("string"==typeof s)l+=C(s),p=f===e.length-1&&-1<u.indexOf(s[s.length-1]);else{var d=s.repeat?"(?:"+s.pattern+")(?:"+C(s.delimiter)+"(?:"+s.pattern+"))*":s.pattern;t&&t.push(s),s.optional?s.partial?l+=C(s.prefix)+"("+d+")?":l+="(?:"+C(s.prefix)+"("+d+"))?":l+=C(s.prefix)+"("+d+")"}}return o?(r||(l+="(?:"+a+")?"),l+="$"===c?"$":"(?="+c+")"):(r||(l+="(?:"+a+"(?="+c+"))?"),p||(l+="(?="+a+"|"+c+")")),new RegExp(l,h(n))}function c(e,t,n){return e instanceof RegExp?function(e,t){if(!t)return e;var n=e.source.match(/\((?!\?)/g);if(n)for(var r=0;r<n.length;r++)t.push({name:r,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,pattern:null});return e}(e,t):Array.isArray(e)?function(e,t,n){for(var r=[],i=0;i<e.length;i++)r.push(c(e[i],t,n).source);return new RegExp("(?:"+r.join("|")+")",h(n))}(e,t,n):function(e,t,n){return u(o(e,n),t,n)}(e,t,n)}w.parse=n,w.compile=t,w.tokensToFunction=r,w.tokensToRegExp=i;function E(e){return"/"===e.charAt(0)?e:"/"+e}function l(e,t){for(var n=e[e.length-1].route.public,r={},i=0,o=e;i<o.length;i++)for(var a=o[i],u=a.route,c=a.parsed,l=u.matching,p=l.keys,f=l.parsers,s=0,d=c.length;s<d;s++){var h=p[s].name,v=f[h]||decodeURIComponent;void 0!==c[s]&&(r[h]=v(c[s]))}return{route:n,match:{location:t,name:n.name,params:r}}}var R=function(e,t,n){void 0===n&&(n={path:"",keys:[]});var r=E(function(e,t){return function(e){return"/"===e.charAt(e.length-1)?e:e+"/"}(e)+t}(n.path,e.path)),i=e.pathOptions||{},o=i.match,a=void 0===o?{}:o,u=i.compile,c=void 0===u?{}:u,l=null==a.end||a.end;e.children&&e.children.length&&(a.end=!1);var p=[],f=w(E(e.path),p,a),s=p.map(function(e){return e.name});n.keys.length&&(s=n.keys.concat(s));var d=[],h=[];if(e.children)for(var v=0,m=e.children;v<m.length;v++){var g=m[v],y=R(g,t,{path:r,keys:s});d.push(y),h.push(y.public)}var x=w.compile(r),b={public:{name:e.name,keys:s,parent:void 0,children:h,methods:{resolve:e.resolve,respond:e.respond,pathname:function(e){return x(e,c)}},extra:e.extra},matching:{re:f,keys:p,exact:l,parsers:e.params||{},children:d}};return t[e.name]=b.public,d.length&&d.forEach(function(e){e.public.parent=b.public}),b},v=function(e,t){var n=e.matching,r=n.re,i=n.children,o=n.exact,a=r.exec(t);if(!a)return[];var u=a[0],c=[{route:e,parsed:a.slice(1)}],l=t.slice(u.length);if(!i.length||""===l)return c;for(var p=E(l),f=0,s=i;f<s.length;f++){var d=s[f],h=v(d,p);if(h.length)return c.concat(h)}return o?[]:c};e.announce=function(t,e){void 0===e&&(e="assertive");var n=document.createElement("div");return n.setAttribute("aria-live",e),n.setAttribute("style",["border: 0 !important;","clip: rect(1px, 1px, 1px, 1px) !important;","-webkit-clip-path: inset(50%) !important;","clip-path: inset(50%) !important;","height: 1px !important;","overflow: hidden !important;","padding: 0 !important;","position: absolute !important;","width: 1px !important;","white-space: nowrap !important;","top: 0;"].join(" ")),document.body.appendChild(n),function(e){n.textContent=t(e)}},e.createRouter=function(e,o,t){var n,r;void 0===t&&(t={});function i(){g=m=void 0}function a(){m&&m(),i()}function u(){g&&g(),i()}function c(){v&&(v=void 0,E.forEach(function(e){e()}))}function l(e,t){if(!e.redirect||!w||A(e.redirect)){var n={response:d=e,navigation:h=t,router:s};!function(t){R.forEach(function(e){e(t)})}(n),function(t){k.splice(0).forEach(function(e){e(t)}),x&&x.forEach(function(e){e(t)})}(n)}void 0===e.redirect||A(e.redirect)||f.navigate(e.redirect,"replace")}function p(e,t,n,r,i){c(),n.finish();var o=T(e,t,i,s,b);u(),l(o,r)}var f,s,d,h,v,m,g,y=void 0===(n=t.history)?{}:n,x=t.sideEffects,b=t.external,w=void 0!==(r=t.invisibleRedirects)&&r,E=[],R=[],k=[];return f=e(function(t){var n={action:t.action,previous:d},e=o.match(t.location);if(!e)return t.finish(),void u();var r=e.route,i=e.match;!function(e){return void 0!==e.methods.resolve}(r)?p(r,i,t,n,null):(E.length&&void 0===v&&(v=function(){f.cancel(),c(),a()},E.forEach(function(e){e(v)})),r.methods.resolve(i,b).then(function(e){return{resolved:e,error:null}},function(e){return{error:e,resolved:null}}).then(function(e){t.cancelled||p(r,i,t,n,e)}))},y),s={route:o.route,history:f,external:b,observe:function(t,e){var n,r=void 0===(n=(e||{}).initial)||n;return R.push(t),d&&r&&t({response:d,navigation:h,router:s}),function(){R=R.filter(function(e){return e!==t})}},once:function(e,t){var n,r=void 0===(n=(t||{}).initial)||n;d&&r?e({response:d,navigation:h,router:s}):k.push(e)},cancel:function(t){return E.push(t),function(){E=E.filter(function(e){return e!==t})}},url:function(e){var t,n=e.name,r=e.params,i=e.hash,o=e.query;if(n){var a=s.route(n);a&&(t=function(e,t){return e.methods.pathname(t)}(a,r))}return f.url({pathname:t,hash:i,query:o})},navigate:function(e){a();var t=e.url,n=e.state,r=e.method;if(f.navigate({url:t,state:n},r),e.cancelled||e.finished)return function(e,t){return m=e,g=t,i}(e.cancelled,e.finished)},current:function(){return{response:d,navigation:h}},destroy:function(){f.destroy()}},f.current(),s},e.prepareRoutes=function(e){var t={},o=e.map(function(e){return R(e,t)});return{match:function(e){for(var t=0,n=o;t<n.length;t++){var r=n[t],i=v(r,e.pathname);if(i.length)return l(i,e)}},route:function(e){return t[e]}}},e.scroll=function(){return function(e){var n=e.response;"pop"!==e.navigation.action&&setTimeout(function(){var e=n.location.hash;if(""!==e){var t=document.getElementById(e);if(t&&t.scrollIntoView)return void t.scrollIntoView()}window.scrollTo(0,0)},0)}},e.title=function(t){return function(e){document.title=t(e)}},Object.defineProperty(e,"__esModule",{value:!0})});