This commit is contained in:
2026-02-10 01:26:18 -05:00
parent 64a2ba2751
commit 6053d4d02c
7 changed files with 367 additions and 436 deletions

32
src/utils/jsonc.ts Normal file
View File

@@ -0,0 +1,32 @@
/**
* JSONC parser utility for handling JSON with comments
*
* JSONC (JSON with Comments) is a superset of JSON that allows single-line
* and multi-line comments, which is useful for configuration files.
*/
/**
* Remove JSONC comments from a string
*/
export function stripComments(jsonString: string): string {
const comments = [
{ pattern: /\/\/.*$/gm, replacement: "" },
{ pattern: /\/\*[\s\S]*?\*\//g, replacement: "" },
];
let result = jsonString;
for (const { pattern, replacement } of comments) {
result = result.replace(pattern, replacement);
}
return result;
}
/**
* Parse JSONC string into a JavaScript object
*/
export function parseJSONC(jsonString: string): unknown {
const stripped = stripComments(jsonString);
return JSON.parse(stripped);
}