UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 4.57 kB
import{get as t,ENV as e,$ as r,teardown as s,batch as a,Context as n,watch as h,nest as i}from"./rvx.min.js";function o(t,e=!0){return"/"===t||""===t?"":(!e&&t.endsWith("/")&&(t=t.slice(0,t.length-1)),t.startsWith("/")?t:"/"+t)}function u(t,e,r=!0){return e=o(e,r),(t=o(t,""===e&&r))+e}function c(t,e,r=!0){const s=o(t,!1);if(e=o(e,r),0===s.length)return e;let a=0;for(;;){const t=s.indexOf("/",a+1),r=t<0?s.length:t,n=s.slice(a,r);if(!(e===n||e.startsWith(n,a)&&"/"===e[a+n.length]))break;if(a=r,t<0)break}let n=0;for(let t=a;t<s.length;t++)"/"===s[t]&&n++;return e=e.slice(a),0===n&&"/"===e?"":"/..".repeat(n)+e}class p{#t;#e;#r;constructor(t,e,r){this.#t=t,this.#e=e,this.#r=r}get root(){return this.#t.root}get parent(){return this.#t}get path(){return t(this.#r)}get query(){return this.#t.query}push(t,e){this.#t.push(u(this.#e,t),e)}replace(t,e){this.#t.replace(u(this.#e,t),e)}}class l{#s;#a;constructor(t,e){this.#s=t,this.#a=e}static from(t){if(void 0===t)return;if("string"==typeof t)return new l(t);const e=new URLSearchParams(t);return new l(e.toString(),e)}get raw(){return this.#s}get params(){return void 0===this.#a&&(this.#a=new URLSearchParams(this.#s)),this.#a}}function v(t){return"string"==typeof t?t:new URLSearchParams(t).toString()}class f{#n=e.current;#r=r(void 0);#h=r(void 0);constructor(t){const e=this.#n,r=t?.parseEvents??["hashchange"],a=this.parse.bind(this);for(const t of r)e.window.addEventListener(t,a,{passive:!0}),s(()=>e.window.removeEventListener(t,a));this.parse()}parse(){a(()=>{const t=this.#n.location.hash.slice(1),e=t.indexOf("?");e<0?(this.#r.value=o(t),this.#h.value=void 0):(this.#r.value=o(t.slice(0,e)),this.#h.value=new l(t.slice(e+1)))})}get root(){return this}get parent(){}get path(){return this.#r.value}get query(){return this.#h.value}push(t,e){this.#n.location.hash=`#${o(t)}${void 0===e?"":`?${"string"==typeof e?e:new URLSearchParams(e)}`}`}replace(t,e){this.push(t,e)}}class g{#n=e.current;#i;#r=r(void 0);#h=r(void 0);constructor(t){const e=this.#n;this.#i=t?.basePath??"";const r=t?.parseEvents??["popstate","rvx:router:update"],a=this.parse.bind(this);for(const t of r)e.window.addEventListener(t,a,{passive:!0}),s(()=>e.window.removeEventListener(t,a));this.parse()}parse(){a(()=>{const t=this.#n;this.#r.value=c(this.#i,t.location.pathname),this.#h.value=t.location.search.length>0?new l(t.location.search.slice(1)):void 0})}#o(t,e){let r=u(this.#i,t)||"/";return void 0!==e&&(r+="?"+v(e)),r}get root(){return this}get parent(){}get path(){return this.#r.value}get query(){return this.#h.value}push(t,e){const r=this.#n;r.history.pushState(null,"",this.#o(t,e)),r.window.dispatchEvent(new r.CustomEvent("rvx:router:update"))}replace(t,e){const r=this.#n;r.history.replaceState(null,"",this.#o(t,e)),r.window.dispatchEvent(new r.CustomEvent("rvx:router:update"))}}class m{#r=r(void 0);#h=r(void 0);constructor(t){this.#r.value=o(t?.path??""),this.#h.value=l.from(t?.query)}get root(){return this}get parent(){}get path(){return this.#r.value}get query(){return this.#h.value}push(t,e){a(()=>{this.#r.value=o(t),this.#h.value=l.from(e)})}replace(t,e){this.push(t,e)}}const d=new n;function w(t,e){for(const r of e)if("string"==typeof r.match){const e="/"===r.match?"":r.match;if(e.endsWith("/")){if(t.startsWith(e)||t===e.slice(0,-1))return{route:r,path:o(t.slice(0,e.length-1)),params:void 0,rest:o(t.slice(e.length))}}else if(e===t)return{route:r,path:t,rest:""}}else if("function"==typeof r.match){const e=r.match(t);if(void 0!==e){let s=t;return!t.startsWith(e.path)||t.length!==e.path.length&&"/"!==t[e.path.length]||(s=o(t.slice(e.path.length))),{...e,route:r,rest:s}}}else{if(!(r.match instanceof RegExp))return{route:r,path:"",rest:t};{const e=r.match.exec(t);if(null!==e){const s=o(e[0],!1);let a=t;return!t.startsWith(s)||t.length!==s.length&&"/"!==t[s.length]||(a=o(t.slice(s.length))),{route:r,path:s,params:e,rest:a}}}}}function y(e,s){const a=r(void 0),n=r(void 0);return h(()=>w(t(e),t(s)),t=>{t?(a.value&&a.value.path===t.path&&a.value.route===t.route||(a.value=t),n.value=t.rest):(a.value=void 0,n.value="")}),{match:()=>a.value,rest:()=>n.value}}function q(t){const e=d.current;if(!e)throw new Error("G3");const r=y(()=>e.path,t);return i(r.match,t=>{if(t)return d.inject(new p(e,t.path,r.rest),()=>t.route.content({params:t.params}))})}function P(t){return q(t.routes)}export{p as ChildRouter,f as HashRouter,g as HistoryRouter,m as MemoryRouter,l as Query,d as ROUTER,P as Routes,v as formatQuery,u as join,w as matchRoute,o as normalize,c as relative,q as routes,y as watchRoutes};