poku
Version:
🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.
55 lines (54 loc) • 2.13 kB
JavaScript
const JSONC = /* @__PURE__ */ (() => {
const toJSON = (content) => {
const offset = content.charCodeAt(0) === 65279 ? 1 : 0, length = content.length;
let result = "", segment = offset, pendingComma = !1, cursor = offset;
for (; cursor < length; ) {
const current = content.charCodeAt(cursor);
if (current === 34) {
for (pendingComma && (result += `,${content.slice(segment, cursor)}`, segment = cursor, pendingComma = !1), cursor++; ; ) {
const closing = content.indexOf('"', cursor);
if (closing === -1) {
cursor = length;
break;
}
let backslashes = 0;
for (let pos = closing - 1; pos >= cursor && content.charCodeAt(pos) === 92; pos--)
backslashes++;
if (cursor = closing + 1, (backslashes & 1) === 0)
break;
}
continue;
}
if (current === 47) {
const next = content.charCodeAt(cursor + 1);
if (next === 47) {
result += content.slice(segment, cursor);
const endOfLine = content.indexOf(`
`, cursor + 2);
cursor = endOfLine === -1 ? length : endOfLine, segment = cursor;
continue;
}
if (next === 42) {
result += content.slice(segment, cursor);
const endOfBlock = content.indexOf("*/", cursor + 2);
cursor = endOfBlock === -1 ? length : endOfBlock + 2, segment = cursor;
continue;
}
}
if (current === 44) {
pendingComma ? result += `,${content.slice(segment, cursor)}` : result += content.slice(segment, cursor), segment = cursor + 1, pendingComma = !0, cursor++;
continue;
}
if (current === 93 || current === 125) {
pendingComma = !1, cursor++;
continue;
}
pendingComma && current > 32 && (result += `,${content.slice(segment, cursor)}`, segment = cursor, pendingComma = !1), cursor++;
}
return pendingComma && (result += ","), result += content.slice(segment, length), result;
};
return { parse: (text) => JSON.parse(toJSON(text)) };
})();
export {
JSONC
};