MCPcopy Create free account
hub / github.com/cortexkit/magic-context / stripTrailingCommas

Function stripTrailingCommas

packages/plugin/src/shared/jsonc-parser.ts:66–107  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

64}
65
66function stripTrailingCommas(content: string): string {
67 let result = "";
68 let inString = false;
69 let escaped = false;
70
71 for (let index = 0; index < content.length; index += 1) {
72 const char = content[index];
73
74 if (inString) {
75 result += char;
76 if (escaped) {
77 escaped = false;
78 } else if (char === "\\") {
79 escaped = true;
80 } else if (char === '"') {
81 inString = false;
82 }
83 continue;
84 }
85
86 if (char === '"') {
87 inString = true;
88 result += char;
89 continue;
90 }
91
92 if (char === ",") {
93 let lookahead = index + 1;
94 while (lookahead < content.length && /\s/.test(content[lookahead] ?? "")) {
95 lookahead += 1;
96 }
97 const next = content[lookahead];
98 if (next === "}" || next === "]") {
99 continue;
100 }
101 }
102
103 result += char;
104 }
105
106 return result;
107}
108
109export function parseJsonc<T = unknown>(content: string): T {
110 const normalized = stripTrailingCommas(stripJsonComments(content));

Callers 1

parseJsoncFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected