CoolFace
Apppublic

Henhouse/workflow-comics

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
parseBadJSON.ts24 linesDownload Raw Back to lib
1import { GeneratedPanels } from "@/types"2 3export function parseBadJSON(jsonLikeString: string): GeneratedPanels {4 5  try {6    return JSON.parse(jsonLikeString) as GeneratedPanels7  } catch (err) {8    var regex = /\{\s*"panel":\s*(\d+),\s*"instructions"\s*:\s*"([^"]+)",\s*"caption":\s*"([^"]*)"\s*\}/gs;9      10    let results = [];11    let match;12    13    while ((match = regex.exec(jsonLikeString)) !== null) {14      let json = {15        panel: Number(match[1]),16        instructions: match[2],17        caption: match[3]18      };19      results.push(json);20    }21    22    return results as GeneratedPanels23  }24}