@dtmd/temper 0.0.2 → 0.0.4

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/dist/src/toml.js DELETED
@@ -1,194 +0,0 @@
1
- /**
2
- * TOML value/key encoding and table layout — a faithful port of `toml_write`
3
- * 0.1.2 (`src/string.rs`) and `toml_edit` 0.22.27's `visit_table`
4
- * (`specs/architecture/20-surface.md`, "Content-faithful, deterministically
5
- * emitted (law 5)"). Shared by the manifest emitter (`emit.ts`) and the lock
6
- * stamper (`lock.ts`) so every `key = value` line and every table header the SDK
7
- * writes is byte-identical to the Rust `toml_edit` output — the manifest, the
8
- * projection frontmatter, and the lock all agree to the byte.
9
- */
10
- /** `ValueMetrics::calculate` — the run-length and escape facts style choice reads. */
11
- function stringMetrics(s) {
12
- let maxSingle = 0;
13
- let maxDouble = 0;
14
- let escapeCodes = false;
15
- let escape = false;
16
- let newline = false;
17
- let prevSingle = 0;
18
- let prevDouble = 0;
19
- for (const ch of s) {
20
- const cp = ch.codePointAt(0);
21
- if (cp === 0x27) {
22
- prevSingle += 1;
23
- maxSingle = Math.max(maxSingle, prevSingle);
24
- }
25
- else {
26
- prevSingle = 0;
27
- }
28
- if (cp === 0x22) {
29
- prevDouble += 1;
30
- maxDouble = Math.max(maxDouble, prevDouble);
31
- }
32
- else {
33
- prevDouble = 0;
34
- }
35
- // The arm order mirrors the Rust match: `\` then `\t` (allowed) then `\n`
36
- // then the general control range.
37
- if (cp === 0x5c)
38
- escape = true;
39
- else if (cp === 0x09) {
40
- /* horizontal tab is always allowed — neutral */
41
- }
42
- else if (cp === 0x0a)
43
- newline = true;
44
- else if (cp <= 0x1f || cp === 0x7f)
45
- escapeCodes = true;
46
- }
47
- return { maxSingle, maxDouble, escapeCodes, escape, newline };
48
- }
49
- /** `TomlStringBuilder::as_default` — the fall-through style preference. */
50
- function chooseEncoding(m) {
51
- // as_basic_pretty
52
- if (!(m.escapeCodes || m.escape || m.maxDouble > 0 || m.newline))
53
- return "basic";
54
- // as_literal
55
- if (!(m.escapeCodes || m.maxSingle > 0 || m.newline))
56
- return "literal";
57
- // as_ml_basic_pretty
58
- if (!(m.escapeCodes || m.escape || m.maxDouble > 2))
59
- return "mlbasic";
60
- // as_ml_literal
61
- if (!(m.escapeCodes || m.maxSingle > 2))
62
- return "mlliteral";
63
- // fallback: the escaped forms
64
- return m.newline ? "mlbasic" : "basic";
65
- }
66
- /** The basic/multiline-basic escaper from `write_toml_value` (the `escaped` branch). */
67
- function escapeBasic(s, isMl) {
68
- const maxSeqDouble = isMl ? 2 : 0;
69
- let out = "";
70
- let seqDouble = 0;
71
- for (const ch of s) {
72
- const cp = ch.codePointAt(0);
73
- if (cp === 0x22) {
74
- seqDouble += 1;
75
- if (seqDouble > maxSeqDouble) {
76
- out += '\\"';
77
- seqDouble = 0;
78
- continue;
79
- }
80
- out += '"';
81
- continue;
82
- }
83
- seqDouble = 0;
84
- switch (cp) {
85
- case 0x08:
86
- out += "\\b";
87
- break;
88
- case 0x09:
89
- out += "\\t";
90
- break;
91
- case 0x0a:
92
- // A literal newline survives inside a multiline string; a basic string
93
- // escapes it.
94
- out += isMl ? "\n" : "\\n";
95
- break;
96
- case 0x0c:
97
- out += "\\f";
98
- break;
99
- case 0x0d:
100
- out += "\\r";
101
- break;
102
- case 0x5c:
103
- out += "\\\\";
104
- break;
105
- default:
106
- if (cp <= 0x1f || cp === 0x7f) {
107
- out += "\\u" + cp.toString(16).toUpperCase().padStart(4, "0");
108
- }
109
- else {
110
- out += ch;
111
- }
112
- }
113
- }
114
- return out;
115
- }
116
- /** A TOML string *value* — the exact bytes `toml_edit`'s `value(String)` emits. */
117
- export function encodeString(s) {
118
- const m = stringMetrics(s);
119
- const enc = chooseEncoding(m);
120
- const delimiter = enc === "literal" ? "'" : enc === "basic" ? '"' : enc === "mlliteral" ? "'''" : '"""';
121
- const isMl = enc === "mlliteral" || enc === "mlbasic";
122
- const escaped = enc === "basic" || enc === "mlbasic";
123
- let out = delimiter;
124
- if (m.newline && isMl)
125
- out += "\n";
126
- out += escaped ? escapeBasic(s, isMl) : s;
127
- out += delimiter;
128
- return out;
129
- }
130
- /** `KeyMetrics::calculate` — whether a key may be bare, and its escape facts. */
131
- function keyMetrics(s) {
132
- let unquoted = s.length > 0;
133
- let single = false;
134
- let double = false;
135
- let escapeCodes = false;
136
- let escape = false;
137
- for (const ch of s) {
138
- const cp = ch.codePointAt(0);
139
- const wordByte = (cp >= 0x61 && cp <= 0x7a) ||
140
- (cp >= 0x41 && cp <= 0x5a) ||
141
- (cp >= 0x30 && cp <= 0x39) ||
142
- cp === 0x2d ||
143
- cp === 0x5f;
144
- if (!wordByte)
145
- unquoted = false;
146
- if (cp === 0x27)
147
- single = true;
148
- else if (cp === 0x22)
149
- double = true;
150
- else if (cp === 0x5c)
151
- escape = true;
152
- else if (cp === 0x09) {
153
- /* tab allowed */
154
- }
155
- else if (cp <= 0x1f || cp === 0x7f)
156
- escapeCodes = true;
157
- }
158
- return { unquoted, single, double, escapeCodes, escape };
159
- }
160
- /** A TOML *key* — bare where it can be, else `toml_edit`'s `TomlKeyBuilder::as_default`. */
161
- export function encodeKey(s) {
162
- const m = keyMetrics(s);
163
- if (m.unquoted)
164
- return s;
165
- // as_basic_pretty
166
- if (!(m.escapeCodes || m.escape || m.double))
167
- return '"' + escapeBasic(s, false) + '"';
168
- // as_literal
169
- if (!(m.escapeCodes || m.single))
170
- return "'" + s + "'";
171
- // as_basic (fallback)
172
- return '"' + escapeBasic(s, false) + '"';
173
- }
174
- /** One `key = value\n` line, the key/value decor `toml_edit` renders (`key = value`). */
175
- export function keyValue(key, valueRepr) {
176
- return `${encodeKey(key)} = ${valueRepr}\n`;
177
- }
178
- /** A TOML string array — `["a", "b"]`, no leading space, `, ` between elements. */
179
- export function stringArray(values) {
180
- return "[" + values.map(encodeString).join(", ") + "]";
181
- }
182
- /** Sorted keys — the stable order `toml_edit` gets for free from its `BTreeMap`s. */
183
- export function sortedKeys(record) {
184
- return Object.keys(record).sort();
185
- }
186
- /**
187
- * Join an ordered list of table sections the `toml_edit` way — exactly one blank
188
- * line before every table header but the document's first
189
- * (`DEFAULT_TABLE_DECOR = ("\n", "")`, the first table `("", …)`). Each section is
190
- * a header line plus its `key = value\n` lines, already newline-terminated.
191
- */
192
- export function joinSections(sections) {
193
- return sections.map((section, i) => (i === 0 ? "" : "\n") + section).join("");
194
- }