Strip enum-style declared types like `enum<["a","b"]>` and return the literal set, or null.
(declared)
| 19 | const logger_1 = require("../logger"); |
| 20 | /** Strip enum-style declared types like `enum<["a","b"]>` and return the literal set, or null. */ |
| 21 | function 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. */ |
| 54 | function declaredPrimitiveCheck(declared, value) { |
| 55 | switch (declared) { |
no outgoing calls
no test coverage detected