( text: string, key: string, )
| 51 | * Returns the first match, or undefined if not found. |
| 52 | */ |
| 53 | export function extractJsonStringField( |
| 54 | text: string, |
| 55 | key: string, |
| 56 | ): string | undefined { |
| 57 | const patterns = [`"${key}":"`, `"${key}": "`] |
| 58 | for (const pattern of patterns) { |
| 59 | const idx = text.indexOf(pattern) |
| 60 | if (idx < 0) continue |
| 61 | |
| 62 | const valueStart = idx + pattern.length |
| 63 | let i = valueStart |
| 64 | while (i < text.length) { |
| 65 | if (text[i] === '\\') { |
| 66 | i += 2 |
| 67 | continue |
| 68 | } |
| 69 | if (text[i] === '"') { |
| 70 | return unescapeJsonString(text.slice(valueStart, i)) |
| 71 | } |
| 72 | i++ |
| 73 | } |
| 74 | } |
| 75 | return undefined |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Like extractJsonStringField but finds the LAST occurrence. |
no test coverage detected