Comparing version 0.5.3 to 0.5.4
@@ -1,2 +0,2 @@ | ||
var i=class{constructor(t,{replace:o,search:e,state:s}={}){this.params=[];var a,c;if(typeof t=="string"){t.startsWith("/")||(t=`/${t}`),e||(this._h=t);let[p,u]=t.split("?");this.path=p.split("/").filter(Boolean),(a=this.search)!=null||(this.search=Object.fromEntries(new URLSearchParams(u).entries()))}else this.path=t;e!==void 0&&(this.search=e),s!==void 0&&(this.state=s),o!==void 0&&(this.replace=o),(c=this.search)!=null||(this.search={})}get href(){if(!this._h){let t=new URLSearchParams(this.search).toString(),o=`/${this.path.join("/")}`;this._h=`${o}${t?`?${t}`:""}`}return this._h}get go(){return(t,o={})=>{var e;return new i(t,{search:o.search,state:o.state,replace:(e=o.replace)!=null?e:this.replace})}}};var R=10,m=async(r,t,o)=>{var a;let e=t,s=new Array;for(;e instanceof i&&s.length<=R;){t=e,s.push(t);let c=(a=P(r,t))!=null?a:[o];for(let p of c)if(e=await p(t,e instanceof i?void 0:e),e instanceof i)break}if(s.length>R)throw new Error(`More than ${R} redirects: ${s.map(c=>c.href).join(" -> ")}`);return{value:e,opts:t}},P=(r,t)=>{let o=[],e=r;for(let s of t.path){if(!e||typeof e=="function")return null;e=w(e,t,s,o)}return typeof e=="function"?(o.unshift(e),o):e&&typeof e[""]=="function"?(o.unshift(e[""]),o):null},w=(r,t,o,e)=>{if(o in r)return typeof r[""]=="function"&&e.unshift(r[""]),r[o];if("*"in r)return t.params.push(o),r["*"];let s=r[""];return!s||typeof s=="function"?null:w(s,t,o,e)};var b=(r={},{notFound:t=({go:a})=>a([]),noClick:o=!1,defer:e=!1,onResolve:s}={})=>{let a,c=new Set(s?[s]:[]),p,u={routes:r,get resolution(){return p},init(){window.addEventListener("popstate",d),o||document.addEventListener("click",T),d({state:history.state})},dispose(){window.removeEventListener("popstate",d),document.removeEventListener("click",T)},async go(n,l){await this.resolution;let f=n instanceof i?n:new i(n,l),v=await g(m(u.routes,f,t));y(v.opts)},onResolve(n){return c.add(n),a&&n(a),()=>c.delete(n)}},T=n=>{var f;let l=O(n.target)?n.target:(f=n.composedPath)==null?void 0:f.call(n).find(O);l&&l.origin===location.origin&&(u.go(l.pathname,{replace:"replace"in l.dataset}),n.preventDefault())},d=async({state:n})=>{let{pathname:l,search:f}=window.location,v=new i(`${l}${f}`,{state:n}),{opts:h}=await g(m(u.routes,v,t));h!==v&&y(new i(h.path,{replace:!0,search:h.search,state:h.state}))},g=async n=>(p=n,a=await n,c.forEach(l=>l(a)),n),y=({state:n,replace:l,href:f})=>{let v=window.history;l?v.replaceState(n,"",f):v.pushState(n,"",f)};return e||u.init(),u},O=r=>r instanceof HTMLAnchorElement;export{i as NavOpts,b as createRouter,m as resolve}; | ||
var a=class{constructor(e,{replace:r,search:s,state:t}={}){this.params=[];var n,i;if(typeof e=="string"){e.startsWith("/")||(e=`/${e}`),s||(this._h=e);let[c,f]=e.split("?");this.path=c.split("/").filter(Boolean),(n=this.search)!=null||(this.search=Object.fromEntries(new URLSearchParams(f).entries()))}else this.path=e;s!==void 0&&(this.search=s),t!==void 0&&(this.state=t),r!==void 0&&(this.replace=r),(i=this.search)!=null||(this.search={})}get href(){if(!this._h){let e=new URLSearchParams(this.search).toString(),r=`/${this.path.join("/")}`;this._h=`${r}${e?`?${e}`:""}`}return this._h}get go(){return(e,r={})=>{var s;return new a(e,{search:r.search,state:r.state,replace:(s=r.replace)!=null?s:this.replace})}}};var R=10,m=async(p,e,r)=>{var n;let s=e,t=new Array;for(;s instanceof a&&t.length<=R;){e=s,t.push(e);let i=(n=O(p,e))!=null?n:[r];for(let c of i)if(s=await c(e,s instanceof a?void 0:s),s instanceof a)break}if(t.length>R)throw new Error(`More than ${R} redirects: ${t.map(i=>i.href).join(" -> ")}`);return{value:s,opts:e}},O=(p,{path:e,params:r})=>{let s=[],t=p;for(let n=0;n<e.length;n++){let i=e[n];if(!t||typeof t=="function")return null;let c=t[""];typeof c=="function"&&s.unshift(c),i in t?t=t[i]:"*"in t?(r.push(i),t=t["*"]):typeof c=="object"&&(t=c,n--)}do if(typeof t=="function")return s.unshift(t),s;while(t=t[""]);return null};var b=(p={},{notFound:e=({go:n})=>n([]),noClick:r=!1,defer:s=!1,onResolve:t}={})=>{let n,i=new Set(t?[t]:[]),c,f={routes:p,get resolution(){return c},init(){window.addEventListener("popstate",d),r||document.addEventListener("click",T),d({state:history.state})},dispose(){window.removeEventListener("popstate",d),document.removeEventListener("click",T)},async go(o,l){await this.resolution;let v=o instanceof a?o:new a(o,l),u=await g(m(f.routes,v,e));y(u.opts)},onResolve(o){return i.add(o),n&&o(n),()=>i.delete(o)}},T=o=>{var v;let l=w(o.target)?o.target:(v=o.composedPath)==null?void 0:v.call(o).find(w);l&&l.origin===location.origin&&(f.go(l.pathname,{replace:"replace"in l.dataset}),o.preventDefault())},d=async({state:o})=>{let{pathname:l,search:v}=window.location,u=new a(`${l}${v}`,{state:o}),{opts:h}=await g(m(f.routes,u,e));h!==u&&y(new a(h.path,{replace:!0,search:h.search,state:h.state}))},g=async o=>(c=o,n=await o,i.forEach(l=>l(n)),o),y=({state:o,replace:l,href:v})=>{let u=window.history;l?u.replaceState(o,"",v):u.pushState(o,"",v)};return s||f.init(),f},w=p=>p instanceof HTMLAnchorElement;export{a as NavOpts,b as createRouter,m as resolve}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "esroute", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "A small efficient framework-agnostic client-side routing library, written in TypeScript.", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
24203