akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
51 lines (50 loc) • 1.67 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* Strip JavaScript-style comments from a JSON string (JSONC support).
* Handles `//` line comments, which end at a line feed or a carriage return,
* and `/* */` block comments while preserving comment-like sequences inside
* quoted strings. A leading UTF-8 byte-order mark, which Windows PowerShell 5.1 writes, is dropped too.
*/
export function stripJsonComments(text) {
let result = "";
let i = Number(text.startsWith("\uFEFF"));
let inString = false;
while (i < text.length) {
if (inString) {
if (text[i] === "\\") {
result += text[i] + (text[i + 1] ?? "");
i += 2;
continue;
}
if (text[i] === '"') {
inString = false;
}
result += text[i];
i++;
continue;
}
if (text[i] === '"') {
inString = true;
result += text[i];
i++;
continue;
}
if (text[i] === "/" && text[i + 1] === "/") {
while (i < text.length && text[i] !== "\n" && text[i] !== "\r")
i++;
continue;
}
if (text[i] === "/" && text[i + 1] === "*") {
i += 2;
while (i < text.length && !(text[i] === "*" && text[i + 1] === "/"))
i++;
i += 2;
continue;
}
result += text[i];
i++;
}
return result;
}