pmsec 2026.11.1 → 2026.11.2
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.
- package/package.json +1 -1
- package/src/util/lines.mjs +86 -17
package/package.json
CHANGED
package/src/util/lines.mjs
CHANGED
|
@@ -1,9 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
// TOML tables may carry a trailing comment, inner whitespace, quoted names, or be [[arrays]].
|
|
2
|
+
const SECTION = /^\s*\[\[?[^\[\]]+\]\]?\s*(#.*)?$/;
|
|
3
|
+
const HEADER = /^\s*\[([^\[\]]+)\]\s*(#.*)?$/;
|
|
4
|
+
const KEY = String.raw`("[^"]*"|'[^']*'|[A-Za-z0-9_.\-]+)`;
|
|
5
|
+
|
|
6
|
+
function unquote(s) {
|
|
7
|
+
return /^(".*"|'.*')$/.test(s) ? s.slice(1, -1) : s;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function headerName(line) {
|
|
11
|
+
const m = line.match(HEADER);
|
|
12
|
+
return m ? m[1].split(".").map(p => unquote(p.trim())).join(".") : null;
|
|
13
|
+
}
|
|
2
14
|
|
|
3
15
|
function entryKey(line, sep) {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
16
|
+
const m = line.match(new RegExp(String.raw`^\s*${KEY}\s*${sep}`));
|
|
17
|
+
return m ? unquote(m[1]) : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Value after the separator with any trailing comment removed.
|
|
21
|
+
function entryValue(line, sep) {
|
|
22
|
+
const m = line.match(new RegExp(String.raw`^\s*${KEY}\s*${sep}\s*(.*?)\s*$`));
|
|
23
|
+
if (!m) return null;
|
|
24
|
+
const v = m[2];
|
|
25
|
+
const close = { '"': '"', "'": "'", "{": "}" }[v[0]];
|
|
26
|
+
if (close) {
|
|
27
|
+
const end = v.indexOf(close, 1);
|
|
28
|
+
if (end >= 0) return v.slice(0, end + 1);
|
|
29
|
+
}
|
|
30
|
+
return v.replace(/\s+#.*$/, "");
|
|
7
31
|
}
|
|
8
32
|
|
|
9
33
|
function rangeForSection(lines, section) {
|
|
@@ -11,10 +35,9 @@ function rangeForSection(lines, section) {
|
|
|
11
35
|
for (let i = 0; i < lines.length; i++) if (SECTION.test(lines[i])) return [0, i];
|
|
12
36
|
return [0, lines.length];
|
|
13
37
|
}
|
|
14
|
-
const header = `[${section}]`;
|
|
15
38
|
let start = -1;
|
|
16
39
|
for (let i = 0; i < lines.length; i++) {
|
|
17
|
-
if (lines[i]
|
|
40
|
+
if (headerName(lines[i]) === section) { start = i + 1; break; }
|
|
18
41
|
}
|
|
19
42
|
if (start < 0) return null;
|
|
20
43
|
let end = lines.length;
|
|
@@ -29,6 +52,34 @@ function indexOfKey(lines, range, key, sep) {
|
|
|
29
52
|
return -1;
|
|
30
53
|
}
|
|
31
54
|
|
|
55
|
+
// TOML forbids `a.b = x` under [s] when [s.a] exists or `a` is an inline table,
|
|
56
|
+
// so target the subtable, or report the inline-table line to edit in place.
|
|
57
|
+
function resolve(lines, key, sep, section) {
|
|
58
|
+
if (section && sep === "=" && key.includes(".")) {
|
|
59
|
+
const dot = key.indexOf(".");
|
|
60
|
+
const head = key.slice(0, dot), rest = key.slice(dot + 1);
|
|
61
|
+
const sub = `${section}.${head}`;
|
|
62
|
+
if (rangeForSection(lines, sub) !== null) return { key: rest, section: sub };
|
|
63
|
+
const range = rangeForSection(lines, section);
|
|
64
|
+
if (range !== null) {
|
|
65
|
+
const i = indexOfKey(lines, range, head, sep);
|
|
66
|
+
if (i >= 0 && (entryValue(lines[i], sep) || "").startsWith("{")) return { key: rest, section, inline: i, head };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { key, section };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function inlinePairs(line) {
|
|
73
|
+
const v = entryValue(line, "=");
|
|
74
|
+
const body = v.slice(1, v.endsWith("}") ? -1 : undefined).trim();
|
|
75
|
+
return body === "" ? [] : body.split(",").map(p => p.trim()).filter(Boolean);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function inlineLine(line, head, pairs) {
|
|
79
|
+
const indent = line.match(/^\s*/)[0];
|
|
80
|
+
return `${indent}${head} = {${pairs.length ? ` ${pairs.join(", ")} ` : ""}}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
32
83
|
function indexOfFirstSection(lines) {
|
|
33
84
|
for (let i = 0; i < lines.length; i++) if (SECTION.test(lines[i])) return i;
|
|
34
85
|
return -1;
|
|
@@ -36,22 +87,33 @@ function indexOfFirstSection(lines) {
|
|
|
36
87
|
|
|
37
88
|
export function readKey(text, key, opts = {}) {
|
|
38
89
|
const sep = opts.sep || "=";
|
|
39
|
-
const section = opts.section || null;
|
|
40
90
|
const lines = text.split(/\r?\n/);
|
|
41
|
-
const
|
|
91
|
+
const r = resolve(lines, key, sep, opts.section || null);
|
|
92
|
+
if (r.inline !== undefined) {
|
|
93
|
+
const pair = inlinePairs(lines[r.inline]).find(p => entryKey(p, sep) === r.key);
|
|
94
|
+
return pair ? entryValue(pair, sep) : null;
|
|
95
|
+
}
|
|
96
|
+
const range = rangeForSection(lines, r.section);
|
|
42
97
|
if (range === null) return null;
|
|
43
|
-
const i = indexOfKey(lines, range, key, sep);
|
|
44
|
-
|
|
45
|
-
const re = sep === ":" ? /^\s*[A-Za-z0-9_.\-]+\s*:\s*(.*?)\s*$/ : /^\s*[A-Za-z0-9_.\-]+\s*=\s*(.*?)\s*$/;
|
|
46
|
-
const m = lines[i].match(re);
|
|
47
|
-
return m ? m[1] : null;
|
|
98
|
+
const i = indexOfKey(lines, range, r.key, sep);
|
|
99
|
+
return i < 0 ? null : entryValue(lines[i], sep);
|
|
48
100
|
}
|
|
49
101
|
|
|
50
102
|
export function setKey(text, key, valueLine, opts = {}) {
|
|
51
103
|
const sep = opts.sep || "=";
|
|
52
|
-
const section = opts.section || null;
|
|
53
104
|
const trailing = text.endsWith("\n") || text === "";
|
|
54
105
|
const lines = text === "" ? [] : text.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
106
|
+
const r = resolve(lines, key, sep, opts.section || null);
|
|
107
|
+
const section = r.section;
|
|
108
|
+
if (r.key !== key) valueLine = r.key + valueLine.slice(key.length);
|
|
109
|
+
if (r.inline !== undefined) {
|
|
110
|
+
const pairs = inlinePairs(lines[r.inline]);
|
|
111
|
+
const at = pairs.findIndex(p => entryKey(p, sep) === r.key);
|
|
112
|
+
if (at >= 0) pairs[at] = valueLine; else pairs.push(valueLine);
|
|
113
|
+
lines[r.inline] = inlineLine(lines[r.inline], r.head, pairs);
|
|
114
|
+
return lines.join("\n") + "\n";
|
|
115
|
+
}
|
|
116
|
+
key = r.key;
|
|
55
117
|
let range = rangeForSection(lines, section);
|
|
56
118
|
if (range === null) {
|
|
57
119
|
if (lines.length && lines[lines.length - 1] !== "") lines.push("");
|
|
@@ -73,12 +135,19 @@ export function setKey(text, key, valueLine, opts = {}) {
|
|
|
73
135
|
|
|
74
136
|
export function removeKey(text, key, opts = {}) {
|
|
75
137
|
const sep = opts.sep || "=";
|
|
76
|
-
const section = opts.section || null;
|
|
77
138
|
const trailing = text.endsWith("\n");
|
|
78
139
|
const lines = text === "" ? [] : text.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
79
|
-
const
|
|
140
|
+
const r = resolve(lines, key, sep, opts.section || null);
|
|
141
|
+
if (r.inline !== undefined) {
|
|
142
|
+
const pairs = inlinePairs(lines[r.inline]);
|
|
143
|
+
const kept = pairs.filter(p => entryKey(p, sep) !== r.key);
|
|
144
|
+
if (kept.length === pairs.length) return { text, removed: false };
|
|
145
|
+
lines[r.inline] = inlineLine(lines[r.inline], r.head, kept);
|
|
146
|
+
return { text: lines.join("\n") + (trailing ? "\n" : ""), removed: true };
|
|
147
|
+
}
|
|
148
|
+
const range = rangeForSection(lines, r.section);
|
|
80
149
|
if (range === null) return { text, removed: false };
|
|
81
|
-
const idx = indexOfKey(lines, range, key, sep);
|
|
150
|
+
const idx = indexOfKey(lines, range, r.key, sep);
|
|
82
151
|
if (idx < 0) return { text, removed: false };
|
|
83
152
|
lines.splice(idx, 1);
|
|
84
153
|
if (idx < lines.length && lines[idx] === "") lines.splice(idx, 1);
|