reminist
Version:
<div align="center">
208 lines (202 loc) • 6.11 kB
JavaScript
"use strict";
var f = Object.defineProperty;
var N = Object.getOwnPropertyDescriptor;
var w = Object.getOwnPropertyNames;
var A = Object.prototype.hasOwnProperty;
var k = (i, t) => {
for (var n in t)
f(i, n, { get: t[n], enumerable: !0 });
}, D = (i, t, n, o) => {
if (t && typeof t == "object" || typeof t == "function")
for (let a of w(t))
!A.call(i, a) && a !== n && f(i, a, { get: () => t[a], enumerable: !(o = N(t, a)) || o.enumerable });
return i;
};
var O = (i) => D(f({}, "__esModule", { value: !0 }), i);
// src/index.ts
var R = {};
k(R, {
Node: () => d,
NodeType: () => y,
Reminist: () => g
});
module.exports = O(R);
// src/utils/nullPrototype.ts
var c = /* @__PURE__ */ (() => {
let i = function() {
};
return i.prototype = /* @__PURE__ */ Object.create(null), Object.freeze(i.prototype), i;
})();
// src/types/node.ts
var y = /* @__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))(y || {});
// 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 C = new c();
function P(i) {
let t = C[i];
if (t) return t;
let n = 0, o = i.length;
i.charCodeAt(0) === 47 && (n = 1), o > 1 && i.charCodeAt(o - 1) === 47 && o--;
let a = i.substring(n, o).split("/");
return a.length === 1 && a[0] === "" ? (C[i] = [], []) : (C[i] = a, a);
}
var g = class i {
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 a = this.keys[n];
this.routers[a] = new d({ name: "/", endpoint: !1 }), this.staticRouter[a] = 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 a = P(n);
if (!a.some((r) => r.startsWith(":") || r.startsWith("*") || r.startsWith("["))) {
this.staticRouter[t] || (this.staticRouter[t] = new c());
let r = new d({ name: n, endpoint: !0, store: o });
this.staticRouter[t][n] = r;
return;
}
let s = this.getRoot(t);
for (let r = 0; r < a.length; r++) {
let l = a[r], h = s.static?.[l];
if (h) {
s = h;
continue;
}
let u = new d({ name: l, endpoint: !1 });
s.addChild(u), s = u;
}
s.endpoint = !0, s.store = o;
}
find(t, n) {
let o = this.staticRouter[t]?.[n];
if (o)
return { node: o, params: {} };
let a = P(n), e = this.getRoot(t), s = {};
for (let l = 0; l < a.length; l++) {
let h = a[l], u = e.static[h];
if (u) {
e = u;
continue;
}
if (e.nonStaticChildCount === 0) return { node: null, params: {} };
let m = e.dynamic;
if (m) {
s[m.paramName] = h, e = m;
continue;
}
let p = e.catchAll;
if (p)
return s[p.paramName] = a.slice(l).join("/"), e = p, { node: e, params: s };
let b = e.optionalCatchAll;
if (b)
return s[b.paramName] = a.slice(l).join("/"), e = b, { node: e, params: s };
let x = e.wildcard;
return x ? (e = x, s["*"] = a.slice(l).join("/"), { node: e, params: s }) : { node: null, params: {} };
}
if (e.endpoint) return { node: e, params: s };
let r = e.optionalCatchAll;
return r && r.endpoint ? (s[r.paramName] = "", { node: r, params: s }) : { 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 = P(n);
if (o.length === 0 && n !== "/") return !1;
let a = [this.getRoot(t)], e = a[0];
for (let s = 0, r = o.length; s < r; s++) {
let l = e.static?.[o[s]];
if (!l) return !1;
e = l, a.push(e);
}
if (!e.endpoint) return !1;
e.endpoint = !1, e.store = void 0;
for (let s = a.length - 1; s > 0; s--) {
let r = a[s], l = a[s - 1];
if (!r.endpoint && r.childCount === 0)
l.removeChild(r);
else
break;
}
return !0;
}
static create(t) {
return {
withData: () => new i(t)
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Node,
NodeType,
Reminist
});