UNPKG

@mitcsutt/kiln-forms

Version:

Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui

81 lines (80 loc) • 2.66 kB
//#region src/schema/core/patterns.ts var e = 200, t = 256, n = 2, r = 10, i = /^\{(\d+)(,(\d*))?\}/; function a(e, t) { let n = e[t]; if (n === "*" || n === "+") return { length: 1, repeats: !0, variable: !0, unbounded: !0 }; if (n === "?") return { length: 1, repeats: !1, variable: !0, unbounded: !1 }; if (n !== "{") return; let r = i.exec(e.slice(t)); if (!r) return; let a = Number(r[1]), o = r[2] !== void 0 && (r[3] ?? "") === "", s = r[2] === void 0 ? a : o ? Infinity : Number(r[3]); return { length: r[0].length, repeats: s > 1, variable: s > a, unbounded: s - a > 10 }; } function o(e, t) { let n = t + 1; for (e[n] === "^" && (n += 1), e[n] === "]" && (n += 1); n < e.length && e[n] !== "]";) n += e[n] === "\\" ? 2 : 1; return n + 1; } function s(e, t) { if (e[t + 1] !== "?") return 1; let n = /^\(\?<(?![=!])[^>]*>/.exec(e.slice(t)); return n ? n[0].length : e[t + 2] === "<" ? 4 : 3; } function c(e) { let t = { nested: !1, repeatedAlternation: !1, unbounded: 0 }, n = [{ quantified: !1, alternation: !1 }], r = () => n[n.length - 1] ?? { quantified: !1, alternation: !1 }, i = 0, c = () => { let n = a(e, i); if (n) return i += n.length, e[i] === "?" && (i += 1), n.unbounded && (t.unbounded += 1), n; }, l = (e) => { e?.variable && (r().quantified = !0); }; for (; i < e.length;) { let a = e[i]; if (a === "\\") i += 2, l(c()); else if (a === "[") i = o(e, i), l(c()); else if (a === "(") i += s(e, i), n.push({ quantified: !1, alternation: !1 }); else if (a === ")") { i += 1; let e = n.length > 1 ? n.pop() : void 0, a = c(); a?.repeats && e?.quantified && (t.nested = !0), a?.repeats && e?.alternation && (t.repeatedAlternation = !0), e?.alternation && (r().alternation = !0), e?.quantified ? r().quantified = !0 : l(a); } else a === "|" ? (i += 1, r().alternation = !0) : (i += 1, a !== "^" && a !== "$" && l(c())); } return t; } function l(e) { if (e.length > 200) return "Pattern is longer than 200 characters"; let t = c(e); if (t.nested) return "Pattern repeats a group that contains a quantifier (e.g. \"(a+)+\"), which can take exponential time"; if (t.repeatedAlternation) return "Pattern repeats a group that contains an alternation (e.g. \"(a|b)+\"), which can take exponential time"; if (t.unbounded > 2) return "Pattern has more than 2 unbounded or wide quantifiers (*, +, {n,} or a range over 10), which can take polynomial time"; } //#endregion export { t as MAX_PATTERN_INPUT, e as MAX_PATTERN_LENGTH, n as MAX_UNBOUNDED_QUANTIFIERS, r as WIDE_RANGE, l as unsafePatternReason }; //# sourceMappingURL=patterns.js.map