(stdout: string)
| 6 | * them. Single-page output (the common case) round-trips through the same path. |
| 7 | */ |
| 8 | export function parsePaginatedArray<T>(stdout: string): T[] { |
| 9 | const trimmed = stdout.trim(); |
| 10 | if (!trimmed) return []; |
| 11 | |
| 12 | const slices: string[] = []; |
| 13 | let depth = 0; |
| 14 | let inString = false; |
| 15 | let escape = false; |
| 16 | let start = -1; |
| 17 | |
| 18 | for (let i = 0; i < trimmed.length; i++) { |
| 19 | const c = trimmed[i]; |
| 20 | if (inString) { |
| 21 | if (escape) { |
| 22 | escape = false; |
| 23 | } else if (c === "\\") { |
| 24 | escape = true; |
| 25 | } else if (c === '"') { |
| 26 | inString = false; |
| 27 | } |
| 28 | continue; |
| 29 | } |
| 30 | if (c === '"') { |
| 31 | inString = true; |
| 32 | continue; |
| 33 | } |
| 34 | if (c === "[" || c === "{") { |
| 35 | if (depth === 0 && c === "[") start = i; |
| 36 | depth++; |
| 37 | } else if (c === "]" || c === "}") { |
| 38 | depth--; |
| 39 | if (depth === 0 && c === "]" && start !== -1) { |
| 40 | slices.push(trimmed.slice(start, i + 1)); |
| 41 | start = -1; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (slices.length === 0) { |
| 47 | return JSON.parse(trimmed) as T[]; |
| 48 | } |
| 49 | |
| 50 | const merged: T[] = []; |
| 51 | for (const slice of slices) { |
| 52 | const page = JSON.parse(slice) as T[]; |
| 53 | if (Array.isArray(page)) merged.push(...page); |
| 54 | } |
| 55 | return merged; |
| 56 | } |
no test coverage detected