@davesheffer/hunch 1.18.1 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ /** Parse JSON with the comments and trailing commas accepted by VS Code JSONC.
2
+ * String-aware scanning keeps comment-looking text and commas inside strings
3
+ * untouched. Callers still validate the returned shape for their own contract. */
4
+ export function parseJsonc(raw) {
5
+ let withoutComments = "";
6
+ let inString = false;
7
+ for (let index = 0; index < raw.length; index += 1) {
8
+ const char = raw[index];
9
+ const next = raw[index + 1];
10
+ if (inString) {
11
+ withoutComments += char;
12
+ if (char === "\\") {
13
+ withoutComments += next ?? "";
14
+ index += 1;
15
+ }
16
+ else if (char === '"') {
17
+ inString = false;
18
+ }
19
+ continue;
20
+ }
21
+ if (char === '"') {
22
+ inString = true;
23
+ withoutComments += char;
24
+ continue;
25
+ }
26
+ if (char === "/" && next === "/") {
27
+ while (index < raw.length && raw[index] !== "\n")
28
+ index += 1;
29
+ withoutComments += "\n";
30
+ continue;
31
+ }
32
+ if (char === "/" && next === "*") {
33
+ index += 2;
34
+ while (index < raw.length && !(raw[index] === "*" && raw[index + 1] === "/"))
35
+ index += 1;
36
+ index += 1;
37
+ continue;
38
+ }
39
+ withoutComments += char;
40
+ }
41
+ let normalized = "";
42
+ inString = false;
43
+ let escaped = false;
44
+ for (let index = 0; index < withoutComments.length; index += 1) {
45
+ const char = withoutComments[index];
46
+ if (inString) {
47
+ normalized += char;
48
+ if (escaped)
49
+ escaped = false;
50
+ else if (char === "\\")
51
+ escaped = true;
52
+ else if (char === '"')
53
+ inString = false;
54
+ continue;
55
+ }
56
+ if (char === '"') {
57
+ inString = true;
58
+ normalized += char;
59
+ continue;
60
+ }
61
+ if (char === ",") {
62
+ let cursor = index + 1;
63
+ while (cursor < withoutComments.length && /\s/.test(withoutComments[cursor]))
64
+ cursor += 1;
65
+ if (withoutComments[cursor] === "}" || withoutComments[cursor] === "]")
66
+ continue;
67
+ }
68
+ normalized += char;
69
+ }
70
+ return JSON.parse(normalized);
71
+ }
72
+ //# sourceMappingURL=jsonc.js.map