reminist
Version:
<div align="center">
182 lines (178 loc) • 5.39 kB
JavaScript
// src/utils/nullPrototype.ts
var c = /* @__PURE__ */ (() => {
let r = function() {
};
return r.prototype = /* @__PURE__ */ Object.create(null), Object.freeze(r.prototype), r;
})();
// src/types/node.ts
var P = /* @__PURE__ */ ((e) => (e[e.Static = 0] = "Static", e[e.Dynamic = 1] = "Dynamic", e[e.CatchAll = 2] = "CatchAll", e[e.OptionalCatchAll = 3] = "OptionalCatchAll", e[e.Wildcard = 4] = "Wildcard", e))(P || {});
// src/controllers/Node.ts
var d = class {
name;
store;
endpoint;
type;
paramName;
static = new c();
dynamic = null;
catchAll = null;
optionalCatchAll = null;
wildcard = null;
childCount = 0;
nonStaticChildCount = 0;
constructor(t) {
this.name = t.name, this.endpoint = t.endpoint, this.store = t.store;
let n = this.name;
n.startsWith(":") ? (this.type = 1, this.paramName = n.substring(1)) : n.startsWith("[[...") ? (this.type = 3, this.paramName = n.substring(5, n.length - 2)) : n.startsWith("[...") ? (this.type = 2, this.paramName = n.substring(4, n.length - 1)) : n.startsWith("[") ? (this.type = 1, this.paramName = n.substring(1, n.length - 1)) : n === "*" ? (this.type = 4, this.paramName = "*") : (this.type = 0, this.paramName = "");
}
addChild(t) {
switch (this.childCount++, t.type !== 0 && this.nonStaticChildCount++, t.type) {
case 0:
this.static || (this.static = new c()), this.static[t.name] = t;
break;
case 1:
this.dynamic = t;
break;
case 2:
this.catchAll = t;
break;
case 3:
this.optionalCatchAll = t;
break;
case 4:
this.wildcard = t;
break;
}
}
removeChild(t) {
switch (this.childCount--, t.type !== 0 && this.nonStaticChildCount--, t.type) {
case 0:
this.static && delete this.static[t.name];
break;
case 1:
this.dynamic = null;
break;
case 2:
this.catchAll = null;
break;
case 3:
this.optionalCatchAll = null;
break;
case 4:
this.wildcard = null;
break;
}
}
};
// src/controllers/Reminist.ts
var f = new c();
function y(r) {
let t = f[r];
if (t) return t;
let n = 0, o = r.length;
r.charCodeAt(0) === 47 && (n = 1), o > 1 && r.charCodeAt(o - 1) === 47 && o--;
let s = r.substring(n, o).split("/");
return s.length === 1 && s[0] === "" ? (f[r] = [], []) : (f[r] = s, s);
}
var g = class r {
keys = [];
routers = new c();
staticRouter = new c();
constructor(t) {
t?.keys && (this.keys = t.keys);
for (let n = 0, o = this.keys.length; n < o; n++) {
let s = this.keys[n];
this.routers[s] = new d({ name: "/", endpoint: !1 }), this.staticRouter[s] = new c();
}
}
getRoot(t) {
let n = this.routers[t];
return n || (n = new d({ name: "/", endpoint: !1 }), this.routers[t] = new d({ name: "/", endpoint: !1 })), n;
}
add(t, n, o) {
let s = y(n);
if (!s.some((i) => i.startsWith(":") || i.startsWith("*") || i.startsWith("["))) {
this.staticRouter[t] || (this.staticRouter[t] = new c());
let i = new d({ name: n, endpoint: !0, store: o });
this.staticRouter[t][n] = i;
return;
}
let a = this.getRoot(t);
for (let i = 0; i < s.length; i++) {
let l = s[i], h = a.static?.[l];
if (h) {
a = h;
continue;
}
let u = new d({ name: l, endpoint: !1 });
a.addChild(u), a = u;
}
a.endpoint = !0, a.store = o;
}
find(t, n) {
let o = this.staticRouter[t]?.[n];
if (o)
return { node: o, params: {} };
let s = y(n), e = this.getRoot(t), a = {};
for (let l = 0; l < s.length; l++) {
let h = s[l], u = e.static[h];
if (u) {
e = u;
continue;
}
if (e.nonStaticChildCount === 0) return { node: null, params: {} };
let m = e.dynamic;
if (m) {
a[m.paramName] = h, e = m;
continue;
}
let p = e.catchAll;
if (p)
return a[p.paramName] = s.slice(l).join("/"), e = p, { node: e, params: a };
let b = e.optionalCatchAll;
if (b)
return a[b.paramName] = s.slice(l).join("/"), e = b, { node: e, params: a };
let C = e.wildcard;
return C ? (e = C, a["*"] = s.slice(l).join("/"), { node: e, params: a }) : { node: null, params: {} };
}
if (e.endpoint) return { node: e, params: a };
let i = e.optionalCatchAll;
return i && i.endpoint ? (a[i.paramName] = "", { node: i, params: a }) : { node: null, params: {} };
}
has(t, n) {
let o = this.find(t, n);
return o.node !== null && o.node.endpoint;
}
delete(t, n) {
if (this.staticRouter[t]?.[n])
return delete this.staticRouter[t][n], !0;
let o = y(n);
if (o.length === 0 && n !== "/") return !1;
let s = [this.getRoot(t)], e = s[0];
for (let a = 0, i = o.length; a < i; a++) {
let l = e.static?.[o[a]];
if (!l) return !1;
e = l, s.push(e);
}
if (!e.endpoint) return !1;
e.endpoint = !1, e.store = void 0;
for (let a = s.length - 1; a > 0; a--) {
let i = s[a], l = s[a - 1];
if (!i.endpoint && i.childCount === 0)
l.removeChild(i);
else
break;
}
return !0;
}
static create(t) {
return {
withData: () => new r(t)
};
}
};
export {
d as Node,
P as NodeType,
g as Reminist
};