UNPKG

@muryp/router-dom

Version:
138 lines (137 loc) 3.58 kB
function y(e) { const t = {}; for (const r in e) { if (!Object.prototype.hasOwnProperty.call(e, r)) continue; const l = r.replace(/^\//, "").split("/"); if (l.length > 1) { const [s, ...o] = l; t[s] || (t[s] = {}), t[s] = { ...t[s], ...y({ [o.join("/")]: e[r] }) }; } else typeof e[r] == "object" && !Array.isArray(e[r]) ? t[l[0]] = y(e[r]) : t[l[0]] = e[r]; } return t; } function U(e) { return Object.keys(e).some((t) => t.startsWith(":")); } function M(e) { return Object.keys(e).filter((t) => t.startsWith(":")); } function u(e, t, r, n = {}) { const l = e[t], s = r[l], o = U(r); if (s === void 0 && !o) return { router: void 0, params: n }; if (t + 1 < e.length) { if (o) { const d = M(r); return n[d[0].replace(":", "")] = l.replace(/^(.*)[#?]/, "$1"), u( e, t + 1, r[d[0]], n ); } return u(e, t + 1, s, n); } if (o) { const d = M(r); return n[d[0].replace(":", "")] = l.replace(/[#|?].*/, ""), { router: r[d[0]], params: n }; } return { router: s, params: n }; } async function v({ routes: e, settings: t }) { var h, w, m; const r = window.location.href, n = { params: {}, query: {}, url: r, id: t.id }, l = r.split("?")[1]; if (l && (n.query = l.split("&").reduce( (i, c) => { const [a, p] = c.split("="); return i[a] = decodeURIComponent(p), i; }, {} )), t.middleware && t.middleware(n) === !1) { const c = (h = window.history.state) == null ? void 0 : h.prevUrl; c && window.history.pushState({}, "", c); return; } const s = new URL(r).hash.replace(/\?.*/, "").split("/"); let o; if ((s.length === 1 || s[1] === "") && (o = e["@home"]), s.shift(), o === void 0) { const i = u(s, 0, e); n.params = i.params, o = i.router; } if (o === void 0) { const i = e["@404"]; if (i != null && i.middleware && i.middleware(n) === !1) { const p = (w = window.history.state) == null ? void 0 : w.prevUrl; p && window.history.pushState({}, "", p); return; } const c = i.component; if (c) { if (typeof c == "string") document.getElementById(t.id).innerHTML = c; else { const a = await c(n); typeof a == "string" && (document.getElementById(t.id).innerHTML = a); } i.script && i.script(n), t.script && t.script(n); } return; } if (o.middleware && o.middleware(n) === !1) { const c = (m = window.history.state) == null ? void 0 : m.prevUrl; c && window.history.pushState({}, "", c); return; } if (o.title) if (typeof o.title == "string") document.title = o.title; else { const i = await o.title(n); typeof i == "string" && (document.title = i); } window.scrollTo({ top: 0, behavior: "smooth" }); const f = o.component; if (f) if (typeof f == "string") document.getElementById(t.id).innerHTML = f; else { const i = await f(n); typeof i == "string" && (document.getElementById(t.id).innerHTML = i); } const d = r.split("#"); if (d.length === 3) { const i = d[2], c = document.getElementById(i); c && c.scrollIntoView(); } o.script && o.script(n), t.script && t.script(n); } function R(e) { window.onpopstate = async () => { v(e); }; } function k(e) { e.routes = y(e.routes), e.routes["@home"] = e.routes[""], delete e.routes[""], e.settings.isFirstRender !== !1 && v(e), R(e); } export { k as default };