Files

19 lines
578 B
JavaScript

// Comments and trailing commas out, so the opencode configs parse as JSON in tests.
export function stripJsonc(text) {
let out = "";
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (c === '"') {
const start = i;
for (i++; i < text.length && text[i] !== '"'; i++) if (text[i] === "\\") i++;
out += text.slice(start, i + 1);
} else if (c === "/" && text[i + 1] === "/") {
while (i < text.length && text[i] !== "\n") i++;
out += "\n";
} else {
out += c;
}
}
return out.replace(/,(\s*[}\]])/g, "$1");
}