MCPcopy
hub / github.com/Doorman11991/smallcode / parseEnumDecl

Function parseEnumDecl

src/compiled/cognition/validate.js:21–52  ·  view source on GitHub ↗

Strip enum-style declared types like `enum<["a","b"]>` and return the literal set, or null.

(declared)

Source from the content-addressed store, hash-verified

19const logger_1 = require("../logger");
20/** Strip enum-style declared types like `enum<["a","b"]>` and return the literal set, or null. */
21function parseEnumDecl(declared) {
22 const m = declared.match(/^enum<\[(.+)\]>$/);
23 if (!m)
24 return null;
25 // Split on top-level commas. Items are JSON-quoted strings.
26 const items = [];
27 let buf = "";
28 let depth = 0;
29 for (const ch of m[1]) {
30 if (ch === "[" || ch === "{" || ch === "(")
31 depth++;
32 if (ch === "]" || ch === "}" || ch === ")")
33 depth--;
34 if (ch === "," && depth === 0) {
35 items.push(buf.trim());
36 buf = "";
37 }
38 else
39 buf += ch;
40 }
41 if (buf.trim())
42 items.push(buf.trim());
43 // Each item is a JSON-quoted string.
44 return items.map(s => {
45 try {
46 return JSON.parse(s);
47 }
48 catch {
49 return s.replace(/^["']|["']$/g, "");
50 }
51 });
52}
53/** True when the declared type is a recognised primitive. */
54function declaredPrimitiveCheck(declared, value) {
55 switch (declared) {

Callers 1

validateSchemaOnlyFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected