-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.min.js
1 lines (1 loc) · 1.3 KB
/
index.min.js
1
var d=({init:n,routes:t,plugins:e,runtime:o})=>{let a=()=>{};n=typeof n=="function"?n:a,e=(e instanceof Array?e:[]).filter(c=>typeof c=="function");var s=a;let r=n()||{};var m=!0;let P=o(c=>{if(!m)return;let g=c.split("?"),w=g.shift(),b=w.split("/").map(decodeURIComponent),U=g.join("?"),{route:y,Params:j}=Object.keys(t).reduce((f,q)=>{let i=q.split("/");if(i.length==b.length){var h=1;let v=b.reduce((u,x,p)=>(u&&(i[p].substr(0,1)==":"?u[i[p].substr(1)]=x:i[p]!==x?u=null:h++),u),{});if(v&&h>f.weight)return{route:q,Params:v,weight:h}}return f},{route:"*",Params:{},weight:0}),R=t[y];typeof R=="function"&&(r.url=c,r.route=y,r.path=w,r.Params=j,r.query=U,e.forEach(f=>f(r)),s(r),s=R(r),s=typeof s!="function"?a:s)})||a;return()=>{m=!1,s(r),P(r)}};var l=n=>{n.Query=n.query.split("&").map(t=>t.split("=")).map(t=>({key:decodeURIComponent(t.shift()),value:decodeURIComponent(t.join("="))})).filter(({key:t})=>t!="").reduce((t,{key:e,value:o})=>(e.substr(e.length-2)=="[]"?(e=e.substr(0,e.length-2),t[e]instanceof Array||(t[e]=[]),t[e].push(o)):t[e]=o,t),{})};var C=({plugins:n,...t})=>d({...t,plugins:[l].concat(n),runtime:e=>{let o=()=>{e((window.location.hash||"#/").substr(1))};return window.addEventListener("hashchange",o),o(),()=>{window.removeEventListener("hashchange",o)}}});export{C as hashRouter,l as queryParser,d as wand};