* Parses a string containing JSON array content. * Handles JSON parsing errors and validates that the result is an array. * @param content The raw string content to parse. * @param sourceDescription A description of the content's source (e.g., filepath) for logging. * @returns The parsed data ar
(content: string, sourceDescription: string)
| 24 | * @returns The parsed data array, or an empty array if parsing fails or it's not an array. |
| 25 | */ |
| 26 | function parseJsonArrayContent(content: string, sourceDescription: string): any[] { |
| 27 | if (!content || !content.trim()) { |
| 28 | console.warn(`Content from ${sourceDescription} is empty or whitespace.`); |
| 29 | return []; |
| 30 | } |
| 31 | try { |
| 32 | const data = JSON.parse(content); |
| 33 | if (Array.isArray(data)) { |
| 34 | return data; |
| 35 | } |
| 36 | console.warn(`Data from ${sourceDescription} is not a JSON array. Treating as empty.`); |
| 37 | } catch (err) { |
| 38 | if (err instanceof SyntaxError) { |
| 39 | console.error(`Error parsing JSON from ${sourceDescription}: ${err.message}.`); |
| 40 | } else { |
| 41 | console.error(`Unexpected error parsing JSON from ${sourceDescription}:`, err); |
| 42 | } |
| 43 | } |
| 44 | return []; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Reads and parses JSON content from a file path. |
no outgoing calls
no test coverage detected