( raw: string | undefined, fallback: number, name?: string )
| 6 | type EnvSource = Record<string, string | undefined>; |
| 7 | |
| 8 | export const toInt = ( |
| 9 | raw: string | undefined, |
| 10 | fallback: number, |
| 11 | name?: string |
| 12 | ): number => { |
| 13 | if (raw === undefined || raw === "") { |
| 14 | return fallback; |
| 15 | } |
| 16 | |
| 17 | const parsed = parseInt(raw, 10); |
| 18 | |
| 19 | if (Number.isNaN(parsed)) { |
| 20 | const label = name ?? "<int>"; |
| 21 | |
| 22 | throw new Error( |
| 23 | `${label}: invalid integer "${raw}". Set a numeric value or unset the variable to use the default (${String(fallback)}).` |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | return parsed; |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * Strict boolean parser. Accepts a small set of canonical truthy/falsy |
no outgoing calls
no test coverage detected