dilswer
Version:
Blazingly fast data validation library with TypeScript integration.
71 lines (70 loc) • 1.82 kB
JavaScript
// src/validation-algorithms/approximate-complexity.ts
function approximateComplexity(t) {
return t._acceptVisitor({
visit(t2, children) {
var _a, _b, _c, _d, _e, _f, _g, _h;
switch (t2.kind) {
case "literal":
case "instanceOf":
case "enumMember":
case "simple": {
return 1;
}
case "stringMatching": {
return 2;
}
case "set":
case "array": {
return 10 + ((_a = children.reduce(
(c, next) => next + c,
0
)) != null ? _a : 0);
}
case "tuple": {
return 1 + ((_b = children.reduce(
(c, next) => next + c,
0
)) != null ? _b : 0);
}
case "circularRef":
case "circular": {
return 0;
}
case "custom": {
return 1;
}
case "dictionary": {
return 3 + ((_c = children.reduce(
(c, next) => next + c,
0
)) != null ? _c : 0);
}
case "enumUnion": {
return (_d = children == null ? void 0 : children.length) != null ? _d : 1;
}
case "union": {
return (_e = children.reduce(
(c, next) => next + c,
0
)) != null ? _e : 0;
}
case "intersection": {
return Math.floor(((_f = children == null ? void 0 : children.length) != null ? _f : 0) / 2) + ((_g = children.reduce(
(c, next) => next + c,
0
)) != null ? _g : 0);
}
case "record": {
return 2 + ((_h = children.reduce(
(c, next) => next.child + c,
0
)) != null ? _h : 0);
}
}
return 0;
}
});
}
export {
approximateComplexity
};