(raw: string | undefined, name?: string)
| 39 | const BOOL_FALSE_TOKENS = new Set(["false", "0", "no", "off", ""]); |
| 40 | |
| 41 | export const toBool = (raw: string | undefined, name?: string): boolean => { |
| 42 | if (raw === undefined) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | const normalized = raw.trim().toLowerCase(); |
| 47 | |
| 48 | if (BOOL_TRUE_TOKENS.has(normalized)) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | if (BOOL_FALSE_TOKENS.has(normalized)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | const label = name ?? "<bool>"; |
| 57 | const accepted = [...BOOL_TRUE_TOKENS, ...BOOL_FALSE_TOKENS] |
| 58 | .filter((token) => token !== "") |
| 59 | .join(", "); |
| 60 | |
| 61 | throw new Error( |
| 62 | `${label}: invalid boolean "${raw}". Accepted (case-insensitive): ${accepted}.` |
| 63 | ); |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * For booleans whose schema default is `true`. Calling `toBool` alone |
no test coverage detected