metano-runtime 3.0.2 → 3.2.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.
Files changed (185) hide show
  1. package/dist/system/char-members.d.ts +23 -0
  2. package/dist/system/char-members.js +46 -0
  3. package/dist/system/collections/hash-core.d.ts +5 -0
  4. package/dist/system/collections/hash-core.js +24 -0
  5. package/dist/system/collections/helpers.d.ts +16 -3
  6. package/dist/system/collections/helpers.js +28 -2
  7. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  8. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  9. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  10. package/dist/system/collections/immutable-list-core.js +19 -1
  11. package/dist/system/collections/immutable-storage.d.ts +2 -0
  12. package/dist/system/collections/immutable-storage.js +6 -1
  13. package/dist/system/collections/interfaces.d.ts +13 -0
  14. package/dist/system/collections/list.d.ts +3 -0
  15. package/dist/system/collections/list.js +7 -0
  16. package/dist/system/collections/read-only-list.d.ts +11 -0
  17. package/dist/system/collections/read-only-list.js +26 -1
  18. package/dist/system/collections/sorted-list.d.ts +5 -0
  19. package/dist/system/collections/sorted-list.js +12 -1
  20. package/dist/system/comparable-compare-to.d.ts +12 -0
  21. package/dist/system/comparable-compare-to.js +20 -0
  22. package/dist/system/decimal-parsing.d.ts +12 -0
  23. package/dist/system/decimal-parsing.js +86 -0
  24. package/dist/system/exceptions/index.d.ts +1 -0
  25. package/dist/system/exceptions/index.js +1 -0
  26. package/dist/system/exceptions/throw-error.d.ts +7 -0
  27. package/dist/system/exceptions/throw-error.js +9 -0
  28. package/dist/system/hash-code.d.ts +2 -1
  29. package/dist/system/hash-code.js +5 -2
  30. package/dist/system/index.d.ts +3 -0
  31. package/dist/system/index.js +3 -0
  32. package/dist/system/int64-math.js +2 -1
  33. package/dist/system/int64.d.ts +6 -0
  34. package/dist/system/int64.js +18 -9
  35. package/dist/system/integer-division.d.ts +12 -4
  36. package/dist/system/integer-division.js +19 -2
  37. package/dist/system/json/index.d.ts +1 -1
  38. package/dist/system/json/json-serializer.d.ts +12 -1
  39. package/dist/system/json/json-serializer.js +29 -0
  40. package/dist/system/json/json-text-writer.d.ts +24 -0
  41. package/dist/system/json/json-text-writer.js +207 -0
  42. package/dist/system/json/number-from-string.js +20 -6
  43. package/dist/system/json/numeric-converters.js +41 -0
  44. package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
  45. package/dist/system/json/ordinal-ignore-case.js +1 -1
  46. package/dist/system/json/serializer-context.d.ts +13 -7
  47. package/dist/system/json/serializer-context.js +23 -11
  48. package/dist/system/json/types.d.ts +20 -0
  49. package/dist/system/linq/grouping.d.ts +12 -0
  50. package/dist/system/linq/grouping.js +42 -0
  51. package/dist/system/linq/index.d.ts +4 -3
  52. package/dist/system/linq/index.js +3 -2
  53. package/dist/system/linq/linq.js +20 -1
  54. package/dist/system/linq/lookup.d.ts +17 -0
  55. package/dist/system/linq/lookup.js +20 -0
  56. package/dist/system/linq/operators.d.ts +12 -4
  57. package/dist/system/linq/operators.js +61 -15
  58. package/dist/system/linq/terminals.d.ts +29 -2
  59. package/dist/system/linq/terminals.js +71 -3
  60. package/dist/system/linq/types.d.ts +27 -4
  61. package/dist/system/math.d.ts +27 -0
  62. package/dist/system/math.js +45 -0
  63. package/dist/system/min-max.d.ts +6 -0
  64. package/dist/system/min-max.js +10 -0
  65. package/dist/system/number-parsing.d.ts +16 -4
  66. package/dist/system/number-parsing.js +21 -4
  67. package/dist/system/string-comparison.d.ts +56 -0
  68. package/dist/system/string-comparison.js +225 -0
  69. package/dist/system/string-hash-code.d.ts +14 -0
  70. package/dist/system/string-hash-code.js +37 -0
  71. package/dist/system/string-members.d.ts +5 -4
  72. package/dist/system/string-members.js +12 -11
  73. package/dist-unchecked/system/char-members.d.ts +23 -0
  74. package/dist-unchecked/system/char-members.js +46 -0
  75. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  76. package/dist-unchecked/system/collections/hash-core.js +24 -0
  77. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  78. package/dist-unchecked/system/collections/helpers.js +28 -2
  79. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  80. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  81. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  82. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  83. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  84. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  85. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  86. package/dist-unchecked/system/collections/list.d.ts +3 -0
  87. package/dist-unchecked/system/collections/list.js +7 -0
  88. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  89. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  90. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  91. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  92. package/dist-unchecked/system/comparable-compare-to.d.ts +12 -0
  93. package/dist-unchecked/system/comparable-compare-to.js +20 -0
  94. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  95. package/dist-unchecked/system/decimal-parsing.js +86 -0
  96. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  97. package/dist-unchecked/system/exceptions/index.js +1 -0
  98. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  99. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  100. package/dist-unchecked/system/hash-code.d.ts +2 -1
  101. package/dist-unchecked/system/hash-code.js +5 -2
  102. package/dist-unchecked/system/index.d.ts +3 -0
  103. package/dist-unchecked/system/index.js +3 -0
  104. package/dist-unchecked/system/int64-math.js +2 -1
  105. package/dist-unchecked/system/int64.d.ts +6 -0
  106. package/dist-unchecked/system/int64.js +18 -9
  107. package/dist-unchecked/system/integer-division.d.ts +12 -4
  108. package/dist-unchecked/system/integer-division.js +19 -2
  109. package/dist-unchecked/system/json/index.d.ts +1 -1
  110. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  111. package/dist-unchecked/system/json/json-serializer.js +29 -0
  112. package/dist-unchecked/system/json/json-text-writer.d.ts +24 -0
  113. package/dist-unchecked/system/json/json-text-writer.js +207 -0
  114. package/dist-unchecked/system/json/number-from-string.js +20 -6
  115. package/dist-unchecked/system/json/numeric-converters.js +41 -0
  116. package/dist-unchecked/system/json/ordinal-ignore-case.d.ts +2 -0
  117. package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
  118. package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
  119. package/dist-unchecked/system/json/serializer-context.js +23 -11
  120. package/dist-unchecked/system/json/types.d.ts +20 -0
  121. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  122. package/dist-unchecked/system/linq/grouping.js +42 -0
  123. package/dist-unchecked/system/linq/index.d.ts +4 -3
  124. package/dist-unchecked/system/linq/index.js +3 -2
  125. package/dist-unchecked/system/linq/linq.js +20 -1
  126. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  127. package/dist-unchecked/system/linq/lookup.js +20 -0
  128. package/dist-unchecked/system/linq/operators.d.ts +12 -4
  129. package/dist-unchecked/system/linq/operators.js +61 -15
  130. package/dist-unchecked/system/linq/terminals.d.ts +29 -2
  131. package/dist-unchecked/system/linq/terminals.js +71 -3
  132. package/dist-unchecked/system/linq/types.d.ts +27 -4
  133. package/dist-unchecked/system/math.d.ts +27 -0
  134. package/dist-unchecked/system/math.js +45 -0
  135. package/dist-unchecked/system/min-max.d.ts +6 -0
  136. package/dist-unchecked/system/min-max.js +10 -0
  137. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  138. package/dist-unchecked/system/number-parsing.js +21 -4
  139. package/dist-unchecked/system/string-comparison.d.ts +56 -0
  140. package/dist-unchecked/system/string-comparison.js +225 -0
  141. package/dist-unchecked/system/string-hash-code.d.ts +14 -0
  142. package/dist-unchecked/system/string-hash-code.js +37 -0
  143. package/dist-unchecked/system/string-members.d.ts +5 -4
  144. package/dist-unchecked/system/string-members.js +12 -11
  145. package/package.json +1 -1
  146. package/src/system/char-members.ts +57 -0
  147. package/src/system/collections/hash-core.ts +31 -0
  148. package/src/system/collections/helpers.ts +39 -4
  149. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  150. package/src/system/collections/immutable-list-core.ts +23 -1
  151. package/src/system/collections/immutable-storage.ts +8 -1
  152. package/src/system/collections/interfaces.ts +16 -0
  153. package/src/system/collections/list.ts +9 -0
  154. package/src/system/collections/read-only-list.ts +35 -1
  155. package/src/system/collections/sorted-list.ts +15 -1
  156. package/src/system/comparable-compare-to.ts +23 -0
  157. package/src/system/decimal-parsing.ts +109 -0
  158. package/src/system/exceptions/index.ts +1 -0
  159. package/src/system/exceptions/throw-error.ts +9 -0
  160. package/src/system/hash-code.ts +7 -2
  161. package/src/system/index.ts +3 -0
  162. package/src/system/int64-math.ts +2 -1
  163. package/src/system/int64.ts +27 -10
  164. package/src/system/integer-division.ts +43 -6
  165. package/src/system/json/index.ts +1 -0
  166. package/src/system/json/json-serializer.ts +42 -0
  167. package/src/system/json/json-text-writer.ts +282 -0
  168. package/src/system/json/number-from-string.ts +26 -6
  169. package/src/system/json/numeric-converters.ts +55 -0
  170. package/src/system/json/ordinal-ignore-case.ts +1 -1
  171. package/src/system/json/serializer-context.ts +28 -11
  172. package/src/system/json/types.ts +21 -0
  173. package/src/system/linq/grouping.ts +77 -0
  174. package/src/system/linq/index.ts +8 -0
  175. package/src/system/linq/linq.ts +23 -1
  176. package/src/system/linq/lookup.ts +34 -0
  177. package/src/system/linq/operators.ts +78 -15
  178. package/src/system/linq/terminals.ts +85 -3
  179. package/src/system/linq/types.ts +32 -0
  180. package/src/system/math.ts +63 -0
  181. package/src/system/min-max.ts +11 -0
  182. package/src/system/number-parsing.ts +24 -4
  183. package/src/system/string-comparison.ts +299 -0
  184. package/src/system/string-hash-code.ts +44 -0
  185. package/src/system/string-members.ts +12 -11
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Writes the tree `JsonSerializer.serialize` returns as the JSON text
3
+ * System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
4
+ * writes with its default options (#741, measured on .NET 10):
5
+ *
6
+ * - no whitespace, unless the context writes indented text (#752): `writeIndented` puts each
7
+ * array element and each property on its own line, indented by `indentSize` copies of
8
+ * `indentCharacter` per level (a space and 2 by default), with `newLine` (`"\n"` by default)
9
+ * between lines, `": "` after a property name, and `[]` / `{}` for an empty container;
10
+ * - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
11
+ * `\r` and `\t` keep their short escapes; every other control character,
12
+ * `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
13
+ * printable ASCII is written `\uXXXX` (upper-case hex, one escape per
14
+ * UTF-16 code unit), and a lone surrogate is written `\uFFFD`;
15
+ * - numbers in .NET's shortest round-trip text (`1E+20`, `1E-07`, `-0`),
16
+ * a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
17
+ * NaN and the infinities throw `ArgumentException`, as STJ throws for them.
18
+ */
19
+
20
+ import { formatDouble } from "../double-format.ts";
21
+ import { ArgumentException } from "../exceptions/argument-exception.ts";
22
+ import { rawJsonTextOf } from "./exact-json-number.ts";
23
+ import type { JsonWriteOptions } from "./types.ts";
24
+
25
+ /** The message STJ throws for a NaN or infinite number written as JSON. */
26
+ const NON_FINITE_NUMBER_MESSAGE =
27
+ ".NET number values such as positive and negative infinity cannot be written as valid JSON. " +
28
+ "To make it work when using 'JsonSerializer', consider specifying " +
29
+ "'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
30
+ "(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
31
+
32
+ /** How the writer lays out containers: the compact text, or one line per element. */
33
+ interface Layout {
34
+ /** The text before each element of a container at `depth`, or `""` when compact. */
35
+ lineStart(depth: number): string;
36
+ /** The text between the last element of a container and its closing bracket. */
37
+ lineEnd(depth: number): string;
38
+ /** The text after a property name's colon. */
39
+ readonly afterColon: string;
40
+ }
41
+
42
+ const COMPACT: Layout = {
43
+ lineStart: () => "",
44
+ lineEnd: () => "",
45
+ afterColon: "",
46
+ };
47
+
48
+ function indentedLayout(options: JsonWriteOptions): Layout {
49
+ const unit = (options.indentCharacter ?? " ").repeat(options.indentSize ?? 2);
50
+ const newLine = options.newLine ?? "\n";
51
+
52
+ return {
53
+ lineStart: (depth) => newLine + unit.repeat(depth),
54
+ lineEnd: (depth) => newLine + unit.repeat(depth - 1),
55
+ afterColon: " ",
56
+ };
57
+ }
58
+
59
+ /**
60
+ * The JSON text of `tree`, as System.Text.Json writes it: compact, or laid out as
61
+ * `options` say (the context's `WriteIndented` family, #752).
62
+ */
63
+ export function writeJsonText(tree: unknown, options?: JsonWriteOptions): string {
64
+ const parts: string[] = [];
65
+ const layout = options?.writeIndented ? indentedLayout(options) : COMPACT;
66
+
67
+ writeValue(tree, parts, layout, 0);
68
+
69
+ return parts.join("");
70
+ }
71
+
72
+ function writeValue(value: unknown, parts: string[], layout: Layout, depth: number): void {
73
+ if (value == null) {
74
+ parts.push("null");
75
+
76
+ return;
77
+ }
78
+
79
+ switch (typeof value) {
80
+ case "string":
81
+ parts.push(quoted(value));
82
+
83
+ return;
84
+ case "number":
85
+ parts.push(numberText(value));
86
+
87
+ return;
88
+ case "bigint":
89
+ case "boolean":
90
+ parts.push(String(value));
91
+
92
+ return;
93
+ }
94
+
95
+ writeObject(value as object, parts, layout, depth);
96
+ }
97
+
98
+ function writeObject(value: object, parts: string[], layout: Layout, depth: number): void {
99
+ const rawText = rawJsonTextOf(value);
100
+
101
+ if (rawText !== undefined) {
102
+ parts.push(rawText);
103
+
104
+ return;
105
+ }
106
+
107
+ const toJson = (value as { toJSON?: unknown }).toJSON;
108
+
109
+ if (typeof toJson === "function") {
110
+ writeValue(toJson.call(value), parts, layout, depth);
111
+
112
+ return;
113
+ }
114
+
115
+ if (Array.isArray(value)) {
116
+ writeArray(value, parts, layout, depth + 1);
117
+
118
+ return;
119
+ }
120
+
121
+ writeProperties(value as Record<string, unknown>, parts, layout, depth + 1);
122
+ }
123
+
124
+ /** `depth` is the nesting level of the array's elements. */
125
+ function writeArray(
126
+ items: readonly unknown[],
127
+ parts: string[],
128
+ layout: Layout,
129
+ depth: number,
130
+ ): void {
131
+ parts.push("[");
132
+
133
+ items.forEach((item, index) => {
134
+ if (index > 0) {
135
+ parts.push(",");
136
+ }
137
+
138
+ parts.push(layout.lineStart(depth));
139
+ writeValue(item, parts, layout, depth);
140
+ });
141
+
142
+ if (items.length > 0) {
143
+ parts.push(layout.lineEnd(depth));
144
+ }
145
+
146
+ parts.push("]");
147
+ }
148
+
149
+ /**
150
+ * An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify`
151
+ * skips it. `depth` is the nesting level of the properties.
152
+ */
153
+ function writeProperties(
154
+ object: Record<string, unknown>,
155
+ parts: string[],
156
+ layout: Layout,
157
+ depth: number,
158
+ ): void {
159
+ let first = true;
160
+
161
+ parts.push("{");
162
+
163
+ for (const key of Object.keys(object)) {
164
+ const member = object[key];
165
+
166
+ if (member === undefined) {
167
+ continue;
168
+ }
169
+
170
+ if (!first) {
171
+ parts.push(",");
172
+ }
173
+
174
+ first = false;
175
+ parts.push(layout.lineStart(depth), quoted(key), ":", layout.afterColon);
176
+ writeValue(member, parts, layout, depth);
177
+ }
178
+
179
+ if (!first) {
180
+ parts.push(layout.lineEnd(depth));
181
+ }
182
+
183
+ parts.push("}");
184
+ }
185
+
186
+ function numberText(value: number): string {
187
+ if (!Number.isFinite(value)) {
188
+ throw new ArgumentException(NON_FINITE_NUMBER_MESSAGE);
189
+ }
190
+
191
+ return formatDouble(value);
192
+ }
193
+
194
+ function quoted(text: string): string {
195
+ for (let index = 0; index < text.length; index++) {
196
+ if (isEscaped(text.charCodeAt(index))) {
197
+ return `"${escaped(text)}"`;
198
+ }
199
+ }
200
+
201
+ return `"${text}"`;
202
+ }
203
+
204
+ /** Whether `JavaScriptEncoder.Default` escapes the UTF-16 code unit `code`. */
205
+ function isEscaped(code: number): boolean {
206
+ switch (code) {
207
+ case 0x22: // "
208
+ case 0x26: // &
209
+ case 0x27: // '
210
+ case 0x2b: // +
211
+ case 0x3c: // <
212
+ case 0x3e: // >
213
+ case 0x5c: // backslash
214
+ case 0x60: // `
215
+ return true;
216
+ default:
217
+ return code < 0x20 || code >= 0x7f;
218
+ }
219
+ }
220
+
221
+ /** The short escape STJ keeps for `code`, or `undefined`. */
222
+ function shortEscapeOf(code: number): string | undefined {
223
+ switch (code) {
224
+ case 0x08:
225
+ return "\\b";
226
+ case 0x09:
227
+ return "\\t";
228
+ case 0x0a:
229
+ return "\\n";
230
+ case 0x0c:
231
+ return "\\f";
232
+ case 0x0d:
233
+ return "\\r";
234
+ case 0x5c:
235
+ return "\\\\";
236
+ default:
237
+ return undefined;
238
+ }
239
+ }
240
+
241
+ function escaped(text: string): string {
242
+ let result = "";
243
+
244
+ for (let index = 0; index < text.length; index++) {
245
+ const code = text.charCodeAt(index);
246
+
247
+ if (isHighSurrogate(code) && isLowSurrogate(text.charCodeAt(index + 1))) {
248
+ result += unicodeEscape(code) + unicodeEscape(text.charCodeAt(index + 1));
249
+ index++;
250
+ } else {
251
+ result += escapedCodeUnit(code);
252
+ }
253
+ }
254
+
255
+ return result;
256
+ }
257
+
258
+ function escapedCodeUnit(code: number): string {
259
+ const shortEscape = shortEscapeOf(code);
260
+
261
+ if (shortEscape !== undefined) {
262
+ return shortEscape;
263
+ }
264
+
265
+ if (isHighSurrogate(code) || isLowSurrogate(code)) {
266
+ return unicodeEscape(0xfffd);
267
+ }
268
+
269
+ return isEscaped(code) ? unicodeEscape(code) : String.fromCharCode(code);
270
+ }
271
+
272
+ function unicodeEscape(code: number): string {
273
+ return `\\u${code.toString(16).toUpperCase().padStart(4, "0")}`;
274
+ }
275
+
276
+ function isHighSurrogate(code: number): boolean {
277
+ return code >= 0xd800 && code <= 0xdbff;
278
+ }
279
+
280
+ function isLowSurrogate(code: number): boolean {
281
+ return code >= 0xdc00 && code <= 0xdfff;
282
+ }
@@ -122,10 +122,13 @@ function isWithinRange(value: bigint, min: bigint, max: bigint): boolean {
122
122
 
123
123
  /**
124
124
  * Reads the floating-point grammar; `staysFinite` rejects a value that
125
- * overflows the target type. The value keeps double precision, as a JSON
126
- * number read into a `Single` does.
125
+ * overflows the target type, and `round` brings the double to the target's
126
+ * precision (a `Single` rounds to single precision, #695).
127
127
  */
128
- function floatingPointReader(staysFinite: FiniteCheck): TextReader<number> {
128
+ function floatingPointReader(
129
+ staysFinite: FiniteCheck,
130
+ round: (value: number) => number,
131
+ ): TextReader<number> {
129
132
  return (text) => {
130
133
  const literal = FLOATING_POINT_LITERALS.get(text);
131
134
 
@@ -141,7 +144,7 @@ function floatingPointReader(staysFinite: FiniteCheck): TextReader<number> {
141
144
 
142
145
  const value = Number(text);
143
146
 
144
- return staysFinite(parts, value) ? value : undefined;
147
+ return staysFinite(parts, value) ? round(value) : undefined;
145
148
  };
146
149
  }
147
150
 
@@ -149,6 +152,23 @@ function fitsDouble(_: RegExpExecArray, value: number): boolean {
149
152
  return Number.isFinite(value);
150
153
  }
151
154
 
155
+ /** The largest `Single`, `(2 − 2^-23) × 2^127`. */
156
+ const SINGLE_MAX_VALUE = 3.4028234663852886e38;
157
+
158
+ /**
159
+ * A finite double that {@link fitsSingle} accepted, rounded to single
160
+ * precision (#695). Only the threshold double itself would round to an
161
+ * infinity (a tie to even); its text was below the threshold, so it is
162
+ * `±Single.MaxValue`.
163
+ */
164
+ function roundToFiniteSingle(value: number): number {
165
+ const single = Math.fround(value);
166
+
167
+ return Number.isFinite(single) || !Number.isFinite(value)
168
+ ? single
169
+ : Math.sign(single) * SINGLE_MAX_VALUE;
170
+ }
171
+
152
172
  /**
153
173
  * Whether the text stays below the `Single` overflow threshold. The double
154
174
  * is already rounded, so it decides every case but one: a double equal to
@@ -272,8 +292,8 @@ const INTEGER_TOKEN_READERS = /* @__PURE__ */ lazyValue(
272
292
  const NUMBER_TEXT_READERS = /* @__PURE__ */ lazyValue(
273
293
  (): Readonly<Record<NumberTypeName, TextReader<number | bigint>>> => ({
274
294
  ...STRICT_INTEGER_READERS(),
275
- "System.Single": floatingPointReader(fitsSingle),
276
- "System.Double": floatingPointReader(fitsDouble),
295
+ "System.Single": floatingPointReader(fitsSingle, roundToFiniteSingle),
296
+ "System.Double": floatingPointReader(fitsDouble, (value) => value),
277
297
  }),
278
298
  );
279
299
 
@@ -5,6 +5,7 @@ import type {
5
5
  PrimitiveDescriptor,
6
6
  } from "./types.ts";
7
7
  import type { ValueConverter } from "./value-converter.ts";
8
+ import { formatSingle } from "../double-format.ts";
8
9
  import { JsonException } from "./errors.ts";
9
10
  import { isDecimalText, readDecimalText } from "./decimal-text.ts";
10
11
  import {
@@ -61,6 +62,9 @@ const passthroughConverter: ValueConverter = {
61
62
  /** The CLR name of a C# `char`, which reads as a one-code-unit `string`. */
62
63
  const CHAR_TYPE_NAME = "System.Char";
63
64
 
65
+ /** The CLR name of a C# `float`, whose values read rounded to single precision. */
66
+ const SINGLE_TYPE_NAME = "System.Single";
67
+
64
68
  /**
65
69
  * A `primitive`: values pass through both ways, except that a JSON string
66
70
  * for a numeric `numberType` is read by System.Text.Json's grammar when the
@@ -79,6 +83,10 @@ export function primitiveConverterOf(
79
83
  return charConverterOf(errorTypeName);
80
84
  }
81
85
 
86
+ if (numberType === SINGLE_TYPE_NAME) {
87
+ return singleConverterOf(readOptions, errorTypeName);
88
+ }
89
+
82
90
  if (numberType === undefined || !readsNumbersFromStrings(readOptions)) {
83
91
  return passthroughConverter;
84
92
  }
@@ -101,6 +109,53 @@ export function primitiveConverterOf(
101
109
  };
102
110
  }
103
111
 
112
+ /**
113
+ * A `float`: a JSON number reads rounded to single precision, as
114
+ * System.Text.Json parses it into a `Single` (#695), and a string reads
115
+ * through the number grammar (which rounds too) when the context reads
116
+ * numbers from strings. A finite value writes the shortest text that reads
117
+ * back as the same `float`, as System.Text.Json writes it (`0.1`, not the
118
+ * double `0.10000000149011612`), through the exact-number leaf. A
119
+ * non-finite value passes through: System.Text.Json throws for it without
120
+ * `AllowNamedFloatingPointLiterals`, which Metano does not mirror.
121
+ */
122
+ function singleConverterOf(
123
+ readOptions: JsonReadOptions | undefined,
124
+ errorTypeName: ErrorTypeName,
125
+ ): ValueConverter {
126
+ const readsStrings = readsNumbersFromStrings(readOptions);
127
+
128
+ return {
129
+ serialize: (value: unknown) =>
130
+ typeof value === "number" && Number.isFinite(value)
131
+ ? writeExactNumber(formatSingle(value), () => Math.fround(value))
132
+ : passthroughConverter.serialize(value),
133
+ deserialize: (value: unknown) => {
134
+ if (typeof value === "number") {
135
+ return Math.fround(value);
136
+ }
137
+
138
+ const exact = exactNumberTextOf(value);
139
+
140
+ if (exact !== undefined) {
141
+ return Math.fround(Number(exact));
142
+ }
143
+
144
+ if (typeof value !== "string" || !readsStrings) {
145
+ return value;
146
+ }
147
+
148
+ const number = readNumberTextAsNumber(value, SINGLE_TYPE_NAME);
149
+
150
+ if (number === undefined) {
151
+ throw cannotConvert(SINGLE_TYPE_NAME, errorTypeName);
152
+ }
153
+
154
+ return number;
155
+ },
156
+ };
157
+ }
158
+
104
159
  /**
105
160
  * A `long` / `ulong` — the `int64` descriptor (spec 012 FR-050), whatever
106
161
  * the read options:
@@ -57,7 +57,7 @@ export function ordinalIgnoreCaseKeyOf(name: string): string {
57
57
  }
58
58
 
59
59
  /** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
60
- function ordinalUppercaseOf(character: string): string {
60
+ export function ordinalUppercaseOf(character: string): string {
61
61
  const exception = ORDINAL_UPPERCASE_EXCEPTIONS().get(character);
62
62
 
63
63
  if (exception !== undefined) {
@@ -4,13 +4,17 @@ import type {
4
4
  SerializerContextOptions,
5
5
  JsonConverter,
6
6
  JsonReadOptions,
7
+ JsonWriteOptions,
7
8
  } from "./types.ts";
8
9
 
9
10
  /** System.Text.Json's default read behavior: no option set. */
10
11
  const DEFAULT_READ_OPTIONS: JsonReadOptions = /* @__PURE__ */ Object.freeze({});
11
12
 
12
- function hasNoOptions(readOptions: JsonReadOptions): boolean {
13
- return Object.keys(readOptions).length === 0;
13
+ /** System.Text.Json's default text formatting: compact text. */
14
+ const DEFAULT_WRITE_OPTIONS: JsonWriteOptions = /* @__PURE__ */ Object.freeze({});
15
+
16
+ function hasNoOptions(options: JsonReadOptions | JsonWriteOptions): boolean {
17
+ return Object.keys(options).length === 0;
14
18
  }
15
19
 
16
20
  /**
@@ -42,9 +46,18 @@ export abstract class SerializerContext {
42
46
  return DEFAULT_READ_OPTIONS;
43
47
  }
44
48
 
49
+ /**
50
+ * The text formatting of every spec this context creates, mirrored from the
51
+ * C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
52
+ * #752). Generated contexts override it only for an indented context.
53
+ */
54
+ protected get writeOptions(): JsonWriteOptions {
55
+ return DEFAULT_WRITE_OPTIONS;
56
+ }
57
+
45
58
  /**
46
59
  * Registers a spec in the internal registry and returns it, with the
47
- * context's read options attached. Intended to be called from lazy getters
60
+ * context's read and write options attached. Intended to be called from lazy getters
48
61
  * in generated subclasses:
49
62
  *
50
63
  * ```ts
@@ -54,7 +67,7 @@ export abstract class SerializerContext {
54
67
  * ```
55
68
  */
56
69
  protected createSpec<T>(spec: TypeSpec<T>): TypeSpec<T> {
57
- const attached = this.withReadOptions(spec);
70
+ const attached = this.withContextOptions(spec);
58
71
 
59
72
  // Only register class-backed types (those with a constructor reference).
60
73
  // [PlainObject] specs have no `type` field — they're accessed via their
@@ -78,21 +91,25 @@ export abstract class SerializerContext {
78
91
  * ```
79
92
  *
80
93
  * Nothing is registered: a value spec has no constructor to resolve. The
81
- * context's read options are attached, as by `createSpec`.
94
+ * context's read and write options are attached, as by `createSpec`.
82
95
  */
83
96
  protected createValueSpec<T>(spec: ValueSpec<T>): ValueSpec<T> {
84
- return this.withReadOptions(spec);
97
+ return this.withContextOptions(spec);
85
98
  }
86
99
 
87
100
  /**
88
- * `spec` with the context's read options, under any the spec declares
89
- * itself. A spec stays as given when neither has one, so default contexts
90
- * keep their specs unchanged.
101
+ * `spec` with the context's read and write options, under any the spec
102
+ * declares itself. A spec stays as given when neither has one, so default
103
+ * contexts keep their specs unchanged.
91
104
  */
92
- private withReadOptions<S extends { readonly readOptions?: JsonReadOptions }>(spec: S): S {
105
+ private withContextOptions<
106
+ S extends { readonly readOptions?: JsonReadOptions; readonly writeOptions?: JsonWriteOptions },
107
+ >(spec: S): S {
93
108
  const readOptions = { ...this.readOptions, ...spec.readOptions };
109
+ const writeOptions = { ...this.writeOptions, ...spec.writeOptions };
110
+ const withReads = hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
94
111
 
95
- return hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
112
+ return hasNoOptions(writeOptions) ? withReads : { ...withReads, writeOptions };
96
113
  }
97
114
 
98
115
  /**
@@ -311,6 +311,8 @@ export interface TypeSpec<T = unknown> {
311
311
  readonly typeName?: string;
312
312
  /** The context's read behavior; absent when it is System.Text.Json's default. */
313
313
  readonly readOptions?: JsonReadOptions;
314
+ /** The context's text formatting; absent when it writes compact text (the default). */
315
+ readonly writeOptions?: JsonWriteOptions;
314
316
  /**
315
317
  * TS names of this contract's properties (its base chain's included) that
316
318
  * a read sets after construction, and only when the payload carries their
@@ -387,6 +389,23 @@ export interface JsonReadOptions {
387
389
  readonly allowOutOfOrderMetadataProperties?: boolean;
388
390
  }
389
391
 
392
+ /**
393
+ * Context-level text formatting mirrored from `[JsonSourceGenerationOptions]`
394
+ * (`WriteIndented`, `IndentCharacter`, `IndentSize`, `NewLine`); only
395
+ * `serializeText` reads it. Every member is optional; an absent member is
396
+ * System.Text.Json's default.
397
+ */
398
+ export interface JsonWriteOptions {
399
+ /** Write each element and property on its own line. */
400
+ readonly writeIndented?: boolean;
401
+ /** The indent character, a space (the default) or a tab. */
402
+ readonly indentCharacter?: " " | "\t";
403
+ /** The indent characters per nesting level, 0 to 127; 2 by default. */
404
+ readonly indentSize?: number;
405
+ /** The line break; `"\n"` by default (System.Text.Json's is the platform's). */
406
+ readonly newLine?: "\n" | "\r\n";
407
+ }
408
+
390
409
  // ─── Value Spec ──────────────────────────────────────────────────────────────
391
410
 
392
411
  /**
@@ -417,6 +436,8 @@ export interface ValueSpec<T = unknown> {
417
436
  readonly typeName?: string;
418
437
  /** The context's read behavior; absent when it is System.Text.Json's default. */
419
438
  readonly readOptions?: JsonReadOptions;
439
+ /** The context's text formatting; absent when it writes compact text (the default). */
440
+ readonly writeOptions?: JsonWriteOptions;
420
441
  }
421
442
 
422
443
  /** Any spec the generated context exposes: an object contract or a value root. */
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The key bucketing `groupBy`, `toLookup` and `join` share: the elements of a source
3
+ * under the equality .NET uses for a key (`EqualityComparer<K>.Default`, or a user
4
+ * comparer), in the order the keys are first seen, each bucket in source order.
5
+ */
6
+ import type { IEqualityComparer } from "../collections/default-comparer.ts";
7
+ import { HashCore } from "../collections/hash-core.ts";
8
+ import type { Grouping } from "./types.ts";
9
+
10
+ /**
11
+ * `comparer` as .NET's `Lookup` uses it: a `null` key hashes to 0 without asking the
12
+ * comparer, which may throw on it (`GetHashCode(string value) => value.Length`).
13
+ */
14
+ function nullSafe<K>(comparer: IEqualityComparer<K> | null | undefined) {
15
+ return comparer === null || comparer === undefined
16
+ ? comparer
17
+ : {
18
+ equals: (a: K, b: K) => comparer.equals(a, b),
19
+ getHashCode: (key: K) =>
20
+ key === null || key === undefined ? 0 : comparer.getHashCode(key),
21
+ };
22
+ }
23
+
24
+ /** Elements of `source` bucketed by `keySelector`; a `null` key is skipped when `skipNullKeys`. */
25
+ export function bucketsOf<T, K, V = T>(
26
+ source: Iterable<T>,
27
+ keySelector: (item: T) => K,
28
+ comparer: IEqualityComparer<K> | null | undefined,
29
+ elementSelector?: (item: T) => V,
30
+ skipNullKeys = false,
31
+ ): HashCore<K, V[]> {
32
+ const buckets = new HashCore<K, V[]>(nullSafe(comparer));
33
+
34
+ if (elementSelector === undefined && !skipNullKeys && comparer) {
35
+ return bucketAll(
36
+ source,
37
+ keySelector,
38
+ buckets as unknown as HashCore<K, T[]>,
39
+ ) as unknown as HashCore<K, V[]>;
40
+ }
41
+
42
+ for (const item of source) {
43
+ const key = keySelector(item);
44
+
45
+ if (skipNullKeys && (key === null || key === undefined)) {
46
+ continue;
47
+ }
48
+
49
+ buckets.append(
50
+ key,
51
+ elementSelector === undefined ? (item as unknown as V) : elementSelector(item),
52
+ );
53
+ }
54
+
55
+ return buckets;
56
+ }
57
+
58
+ function bucketAll<T, K>(
59
+ source: Iterable<T>,
60
+ keySelector: (item: T) => K,
61
+ buckets: HashCore<K, T[]>,
62
+ ): HashCore<K, T[]> {
63
+ for (const item of source) {
64
+ buckets.append(keySelector(item), item);
65
+ }
66
+
67
+ return buckets;
68
+ }
69
+
70
+ /** The groupings of `buckets`: an iterable bucket that also reads `.key`. */
71
+ export function* groupingsOf<K, T>(buckets: HashCore<K, T[]>): Generator<Grouping<K, T>> {
72
+ for (const [key, items] of buckets) {
73
+ // `Grouping` is `Iterable<T> & { key }`: the bucket array itself carries the key, so
74
+ // consumers can `for…of` the grouping and read `.key`.
75
+ yield Object.assign(items, { key }) as unknown as Grouping<K, T>;
76
+ }
77
+ }
@@ -110,6 +110,9 @@ export type {
110
110
  ToListTerm,
111
111
  ToDictionaryTerm,
112
112
  ToHashSetTerm,
113
+ ToLookupTerm,
114
+ SequenceEqualTerm,
115
+ JoinOp,
113
116
  } from "./types.ts";
114
117
 
115
118
  export {
@@ -134,6 +137,7 @@ export {
134
137
  thenBy,
135
138
  thenByDescending,
136
139
  groupBy,
140
+ join,
137
141
  zip,
138
142
  defaultIfEmpty,
139
143
  } from "./operators.ts";
@@ -145,6 +149,8 @@ export {
145
149
  toList,
146
150
  toDictionary,
147
151
  toHashSet,
152
+ toLookup,
153
+ sequenceEqual,
148
154
  first,
149
155
  firstOrDefault,
150
156
  last,
@@ -183,5 +189,7 @@ export {
183
189
  sumInt64,
184
190
  } from "./int64-terminals.ts";
185
191
 
192
+ export { Lookup } from "./lookup.ts";
193
+
186
194
  export { averageDecimal, averageNullableDecimal, sumDecimal } from "./decimal-terminals.ts";
187
195
  export type { DecimalConstructor, DecimalValue } from "./decimal-terminals.ts";