( config: string, section: string, subsection: string | null, key: string, )
| 36 | * Exported for testing. |
| 37 | */ |
| 38 | export function parseConfigString( |
| 39 | config: string, |
| 40 | section: string, |
| 41 | subsection: string | null, |
| 42 | key: string, |
| 43 | ): string | null { |
| 44 | const lines = config.split('\n') |
| 45 | const sectionLower = section.toLowerCase() |
| 46 | const keyLower = key.toLowerCase() |
| 47 | |
| 48 | let inSection = false |
| 49 | for (const line of lines) { |
| 50 | const trimmed = line.trim() |
| 51 | |
| 52 | // Skip empty lines and comment-only lines |
| 53 | if (trimmed.length === 0 || trimmed[0] === '#' || trimmed[0] === ';') { |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | // Section header |
| 58 | if (trimmed[0] === '[') { |
| 59 | inSection = matchesSectionHeader(trimmed, sectionLower, subsection) |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | if (!inSection) { |
| 64 | continue |
| 65 | } |
| 66 | |
| 67 | // Key-value line: find the key name |
| 68 | const parsed = parseKeyValue(trimmed) |
| 69 | if (parsed && parsed.key.toLowerCase() === keyLower) { |
| 70 | return parsed.value |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return null |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Parse a key = value line. Returns null if the line doesn't contain a valid key. |
no test coverage detected