MCPcopy Create free account
hub / github.com/YGYOOO/WorldX / extractJSONObject

Function extractJSONObject

shared/structured-output.mjs:48–84  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

46}
47
48export function extractJSONObject(text) {
49 const start = text.indexOf("{");
50 if (start === -1) return null;
51
52 let depth = 0;
53 let inString = false;
54 let escaping = false;
55
56 for (let i = start; i < text.length; i++) {
57 const ch = text[i];
58
59 if (inString) {
60 if (escaping) {
61 escaping = false;
62 } else if (ch === "\\") {
63 escaping = true;
64 } else if (ch === "\"") {
65 inString = false;
66 }
67 continue;
68 }
69
70 if (ch === "\"") {
71 inString = true;
72 continue;
73 }
74
75 if (ch === "{") depth++;
76 if (ch === "}") depth--;
77
78 if (depth === 0) {
79 return text.slice(start, i + 1);
80 }
81 }
82
83 return text.slice(start);
84}
85
86export function escapeControlCharsInStrings(text) {
87 let result = "";

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected