metano-runtime 3.0.2 → 3.1.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.
- package/dist/system/char-members.d.ts +23 -0
- package/dist/system/char-members.js +46 -0
- package/dist/system/collections/hash-core.d.ts +5 -0
- package/dist/system/collections/hash-core.js +24 -0
- package/dist/system/collections/helpers.d.ts +16 -3
- package/dist/system/collections/helpers.js +28 -2
- package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist/system/collections/immutable-list-builder-core.js +11 -0
- package/dist/system/collections/immutable-list-core.d.ts +8 -0
- package/dist/system/collections/immutable-list-core.js +19 -1
- package/dist/system/collections/immutable-storage.d.ts +2 -0
- package/dist/system/collections/immutable-storage.js +6 -1
- package/dist/system/collections/interfaces.d.ts +13 -0
- package/dist/system/collections/list.d.ts +3 -0
- package/dist/system/collections/list.js +7 -0
- package/dist/system/collections/read-only-list.d.ts +11 -0
- package/dist/system/collections/read-only-list.js +26 -1
- package/dist/system/collections/sorted-list.d.ts +5 -0
- package/dist/system/collections/sorted-list.js +12 -1
- package/dist/system/decimal-parsing.d.ts +12 -0
- package/dist/system/decimal-parsing.js +86 -0
- package/dist/system/exceptions/index.d.ts +1 -0
- package/dist/system/exceptions/index.js +1 -0
- package/dist/system/exceptions/throw-error.d.ts +7 -0
- package/dist/system/exceptions/throw-error.js +9 -0
- package/dist/system/int64.d.ts +6 -0
- package/dist/system/int64.js +18 -9
- package/dist/system/integer-division.d.ts +12 -4
- package/dist/system/integer-division.js +19 -2
- package/dist/system/json/json-serializer.d.ts +12 -1
- package/dist/system/json/json-serializer.js +28 -0
- package/dist/system/json/json-text-writer.d.ts +17 -0
- package/dist/system/json/json-text-writer.js +175 -0
- package/dist/system/json/number-from-string.js +20 -6
- package/dist/system/json/numeric-converters.js +41 -0
- package/dist/system/linq/grouping.d.ts +12 -0
- package/dist/system/linq/grouping.js +42 -0
- package/dist/system/linq/index.d.ts +4 -3
- package/dist/system/linq/index.js +3 -2
- package/dist/system/linq/linq.js +20 -1
- package/dist/system/linq/lookup.d.ts +17 -0
- package/dist/system/linq/lookup.js +20 -0
- package/dist/system/linq/operators.d.ts +12 -4
- package/dist/system/linq/operators.js +61 -15
- package/dist/system/linq/terminals.d.ts +29 -2
- package/dist/system/linq/terminals.js +71 -3
- package/dist/system/linq/types.d.ts +27 -4
- package/dist/system/math.d.ts +7 -0
- package/dist/system/math.js +14 -0
- package/dist/system/number-parsing.d.ts +16 -4
- package/dist/system/number-parsing.js +21 -4
- package/dist-unchecked/system/char-members.d.ts +23 -0
- package/dist-unchecked/system/char-members.js +46 -0
- package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
- package/dist-unchecked/system/collections/hash-core.js +24 -0
- package/dist-unchecked/system/collections/helpers.d.ts +16 -3
- package/dist-unchecked/system/collections/helpers.js +28 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
- package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
- package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
- package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
- package/dist-unchecked/system/collections/immutable-storage.js +6 -1
- package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
- package/dist-unchecked/system/collections/list.d.ts +3 -0
- package/dist-unchecked/system/collections/list.js +7 -0
- package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
- package/dist-unchecked/system/collections/read-only-list.js +26 -1
- package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
- package/dist-unchecked/system/collections/sorted-list.js +12 -1
- package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
- package/dist-unchecked/system/decimal-parsing.js +86 -0
- package/dist-unchecked/system/exceptions/index.d.ts +1 -0
- package/dist-unchecked/system/exceptions/index.js +1 -0
- package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
- package/dist-unchecked/system/exceptions/throw-error.js +9 -0
- package/dist-unchecked/system/int64.d.ts +6 -0
- package/dist-unchecked/system/int64.js +18 -9
- package/dist-unchecked/system/integer-division.d.ts +12 -4
- package/dist-unchecked/system/integer-division.js +19 -2
- package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
- package/dist-unchecked/system/json/json-serializer.js +28 -0
- package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
- package/dist-unchecked/system/json/json-text-writer.js +175 -0
- package/dist-unchecked/system/json/number-from-string.js +20 -6
- package/dist-unchecked/system/json/numeric-converters.js +41 -0
- package/dist-unchecked/system/linq/grouping.d.ts +12 -0
- package/dist-unchecked/system/linq/grouping.js +42 -0
- package/dist-unchecked/system/linq/index.d.ts +4 -3
- package/dist-unchecked/system/linq/index.js +3 -2
- package/dist-unchecked/system/linq/linq.js +20 -1
- package/dist-unchecked/system/linq/lookup.d.ts +17 -0
- package/dist-unchecked/system/linq/lookup.js +20 -0
- package/dist-unchecked/system/linq/operators.d.ts +12 -4
- package/dist-unchecked/system/linq/operators.js +61 -15
- package/dist-unchecked/system/linq/terminals.d.ts +29 -2
- package/dist-unchecked/system/linq/terminals.js +71 -3
- package/dist-unchecked/system/linq/types.d.ts +27 -4
- package/dist-unchecked/system/math.d.ts +7 -0
- package/dist-unchecked/system/math.js +14 -0
- package/dist-unchecked/system/number-parsing.d.ts +16 -4
- package/dist-unchecked/system/number-parsing.js +21 -4
- package/package.json +1 -1
- package/src/system/char-members.ts +57 -0
- package/src/system/collections/hash-core.ts +31 -0
- package/src/system/collections/helpers.ts +39 -4
- package/src/system/collections/immutable-list-builder-core.ts +16 -2
- package/src/system/collections/immutable-list-core.ts +23 -1
- package/src/system/collections/immutable-storage.ts +8 -1
- package/src/system/collections/interfaces.ts +16 -0
- package/src/system/collections/list.ts +9 -0
- package/src/system/collections/read-only-list.ts +35 -1
- package/src/system/collections/sorted-list.ts +15 -1
- package/src/system/decimal-parsing.ts +109 -0
- package/src/system/exceptions/index.ts +1 -0
- package/src/system/exceptions/throw-error.ts +9 -0
- package/src/system/int64.ts +27 -10
- package/src/system/integer-division.ts +43 -6
- package/src/system/json/json-serializer.ts +41 -0
- package/src/system/json/json-text-writer.ts +224 -0
- package/src/system/json/number-from-string.ts +26 -6
- package/src/system/json/numeric-converters.ts +55 -0
- package/src/system/linq/grouping.ts +77 -0
- package/src/system/linq/index.ts +8 -0
- package/src/system/linq/linq.ts +23 -1
- package/src/system/linq/lookup.ts +34 -0
- package/src/system/linq/operators.ts +78 -15
- package/src/system/linq/terminals.ts +85 -3
- package/src/system/linq/types.ts +32 -0
- package/src/system/math.ts +19 -0
- package/src/system/number-parsing.ts +24 -4
|
@@ -0,0 +1,224 @@
|
|
|
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;
|
|
7
|
+
* - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
|
|
8
|
+
* `\r` and `\t` keep their short escapes; every other control character,
|
|
9
|
+
* `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
|
|
10
|
+
* printable ASCII is written `\uXXXX` (upper-case hex, one escape per
|
|
11
|
+
* UTF-16 code unit), and a lone surrogate is written `\uFFFD`;
|
|
12
|
+
* - numbers in .NET's shortest round-trip text (`1E+20`, `1E-07`, `-0`),
|
|
13
|
+
* a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
|
|
14
|
+
* NaN and the infinities throw `ArgumentException`, as STJ throws for them.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { formatDouble } from "../double-format.ts";
|
|
18
|
+
import { ArgumentException } from "../exceptions/argument-exception.ts";
|
|
19
|
+
import { rawJsonTextOf } from "./exact-json-number.ts";
|
|
20
|
+
|
|
21
|
+
/** The message STJ throws for a NaN or infinite number written as JSON. */
|
|
22
|
+
const NON_FINITE_NUMBER_MESSAGE =
|
|
23
|
+
".NET number values such as positive and negative infinity cannot be written as valid JSON. " +
|
|
24
|
+
"To make it work when using 'JsonSerializer', consider specifying " +
|
|
25
|
+
"'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
|
|
26
|
+
"(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
|
|
27
|
+
|
|
28
|
+
/** The JSON text of `tree`, as System.Text.Json writes it. */
|
|
29
|
+
export function writeJsonText(tree: unknown): string {
|
|
30
|
+
const parts: string[] = [];
|
|
31
|
+
|
|
32
|
+
writeValue(tree, parts);
|
|
33
|
+
|
|
34
|
+
return parts.join("");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeValue(value: unknown, parts: string[]): void {
|
|
38
|
+
if (value == null) {
|
|
39
|
+
parts.push("null");
|
|
40
|
+
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
switch (typeof value) {
|
|
45
|
+
case "string":
|
|
46
|
+
parts.push(quoted(value));
|
|
47
|
+
|
|
48
|
+
return;
|
|
49
|
+
case "number":
|
|
50
|
+
parts.push(numberText(value));
|
|
51
|
+
|
|
52
|
+
return;
|
|
53
|
+
case "bigint":
|
|
54
|
+
case "boolean":
|
|
55
|
+
parts.push(String(value));
|
|
56
|
+
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
writeObject(value as object, parts);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeObject(value: object, parts: string[]): void {
|
|
64
|
+
const rawText = rawJsonTextOf(value);
|
|
65
|
+
|
|
66
|
+
if (rawText !== undefined) {
|
|
67
|
+
parts.push(rawText);
|
|
68
|
+
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const toJson = (value as { toJSON?: unknown }).toJSON;
|
|
73
|
+
|
|
74
|
+
if (typeof toJson === "function") {
|
|
75
|
+
writeValue(toJson.call(value), parts);
|
|
76
|
+
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
writeArray(value, parts);
|
|
82
|
+
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
writeProperties(value as Record<string, unknown>, parts);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function writeArray(items: readonly unknown[], parts: string[]): void {
|
|
90
|
+
parts.push("[");
|
|
91
|
+
|
|
92
|
+
items.forEach((item, index) => {
|
|
93
|
+
if (index > 0) {
|
|
94
|
+
parts.push(",");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
writeValue(item, parts);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
parts.push("]");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify` skips it. */
|
|
104
|
+
function writeProperties(object: Record<string, unknown>, parts: string[]): void {
|
|
105
|
+
let first = true;
|
|
106
|
+
|
|
107
|
+
parts.push("{");
|
|
108
|
+
|
|
109
|
+
for (const key of Object.keys(object)) {
|
|
110
|
+
const member = object[key];
|
|
111
|
+
|
|
112
|
+
if (member === undefined) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!first) {
|
|
117
|
+
parts.push(",");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
first = false;
|
|
121
|
+
parts.push(quoted(key), ":");
|
|
122
|
+
writeValue(member, parts);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
parts.push("}");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function numberText(value: number): string {
|
|
129
|
+
if (!Number.isFinite(value)) {
|
|
130
|
+
throw new ArgumentException(NON_FINITE_NUMBER_MESSAGE);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return formatDouble(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function quoted(text: string): string {
|
|
137
|
+
for (let index = 0; index < text.length; index++) {
|
|
138
|
+
if (isEscaped(text.charCodeAt(index))) {
|
|
139
|
+
return `"${escaped(text)}"`;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return `"${text}"`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Whether `JavaScriptEncoder.Default` escapes the UTF-16 code unit `code`. */
|
|
147
|
+
function isEscaped(code: number): boolean {
|
|
148
|
+
switch (code) {
|
|
149
|
+
case 0x22: // "
|
|
150
|
+
case 0x26: // &
|
|
151
|
+
case 0x27: // '
|
|
152
|
+
case 0x2b: // +
|
|
153
|
+
case 0x3c: // <
|
|
154
|
+
case 0x3e: // >
|
|
155
|
+
case 0x5c: // backslash
|
|
156
|
+
case 0x60: // `
|
|
157
|
+
return true;
|
|
158
|
+
default:
|
|
159
|
+
return code < 0x20 || code >= 0x7f;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The short escape STJ keeps for `code`, or `undefined`. */
|
|
164
|
+
function shortEscapeOf(code: number): string | undefined {
|
|
165
|
+
switch (code) {
|
|
166
|
+
case 0x08:
|
|
167
|
+
return "\\b";
|
|
168
|
+
case 0x09:
|
|
169
|
+
return "\\t";
|
|
170
|
+
case 0x0a:
|
|
171
|
+
return "\\n";
|
|
172
|
+
case 0x0c:
|
|
173
|
+
return "\\f";
|
|
174
|
+
case 0x0d:
|
|
175
|
+
return "\\r";
|
|
176
|
+
case 0x5c:
|
|
177
|
+
return "\\\\";
|
|
178
|
+
default:
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function escaped(text: string): string {
|
|
184
|
+
let result = "";
|
|
185
|
+
|
|
186
|
+
for (let index = 0; index < text.length; index++) {
|
|
187
|
+
const code = text.charCodeAt(index);
|
|
188
|
+
|
|
189
|
+
if (isHighSurrogate(code) && isLowSurrogate(text.charCodeAt(index + 1))) {
|
|
190
|
+
result += unicodeEscape(code) + unicodeEscape(text.charCodeAt(index + 1));
|
|
191
|
+
index++;
|
|
192
|
+
} else {
|
|
193
|
+
result += escapedCodeUnit(code);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function escapedCodeUnit(code: number): string {
|
|
201
|
+
const shortEscape = shortEscapeOf(code);
|
|
202
|
+
|
|
203
|
+
if (shortEscape !== undefined) {
|
|
204
|
+
return shortEscape;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (isHighSurrogate(code) || isLowSurrogate(code)) {
|
|
208
|
+
return unicodeEscape(0xfffd);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return isEscaped(code) ? unicodeEscape(code) : String.fromCharCode(code);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function unicodeEscape(code: number): string {
|
|
215
|
+
return `\\u${code.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function isHighSurrogate(code: number): boolean {
|
|
219
|
+
return code >= 0xd800 && code <= 0xdbff;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isLowSurrogate(code: number): boolean {
|
|
223
|
+
return code >= 0xdc00 && code <= 0xdfff;
|
|
224
|
+
}
|
|
@@ -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
|
|
126
|
-
*
|
|
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(
|
|
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:
|
|
@@ -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
|
+
}
|
package/src/system/linq/index.ts
CHANGED
|
@@ -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";
|
package/src/system/linq/linq.ts
CHANGED
|
@@ -1760,7 +1760,8 @@ export function linq(
|
|
|
1760
1760
|
source: unknown,
|
|
1761
1761
|
...ops: (OperatorBase<any, any> | TerminalBase<any, any>)[]
|
|
1762
1762
|
): unknown {
|
|
1763
|
-
const
|
|
1763
|
+
const start = typeof source === "string" ? codeUnitsOf(source) : source;
|
|
1764
|
+
const result = ops.reduce((acc, op) => op.apply(acc as Iterable<any>) as any, start as any);
|
|
1764
1765
|
// Attach the original stage list to the materialized result so an
|
|
1765
1766
|
// IQueryable-style consumer can introspect the chain. Skipped for
|
|
1766
1767
|
// primitive results (terminals like `count` / `sum` return numbers
|
|
@@ -1814,3 +1815,24 @@ export function stages(...ops: Stage[]): Stage[] {
|
|
|
1814
1815
|
return ops;
|
|
1815
1816
|
}
|
|
1816
1817
|
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1818
|
+
|
|
1819
|
+
/**
|
|
1820
|
+
* The UTF-16 code units of `text`, read lazily: a C# string enumerates them,
|
|
1821
|
+
* where a JS string's own iterator yields code points, so a surrogate pair
|
|
1822
|
+
* would be one element (#695). No array is built, so an early-exit operator
|
|
1823
|
+
* (`first`, `any`, `take`) reads only the units it needs.
|
|
1824
|
+
*/
|
|
1825
|
+
function codeUnitsOf(text: string): Iterable<string> {
|
|
1826
|
+
return {
|
|
1827
|
+
[Symbol.iterator]: () => {
|
|
1828
|
+
let index = 0;
|
|
1829
|
+
|
|
1830
|
+
return {
|
|
1831
|
+
next: (): IteratorResult<string> =>
|
|
1832
|
+
index < text.length
|
|
1833
|
+
? { done: false, value: text[index++]! }
|
|
1834
|
+
: { done: true, value: undefined },
|
|
1835
|
+
};
|
|
1836
|
+
},
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `System.Linq.ILookup<TKey, TElement>`, the result of `ToLookup`: the elements grouped by
|
|
3
|
+
* key, enumerated as groupings in first-seen key order. Keys are compared as `groupBy`
|
|
4
|
+
* compares them.
|
|
5
|
+
*/
|
|
6
|
+
import type { HashCore } from "../collections/hash-core.ts";
|
|
7
|
+
import { groupingsOf } from "./grouping.ts";
|
|
8
|
+
import type { Grouping } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
export class Lookup<K, T> implements Iterable<Grouping<K, T>> {
|
|
11
|
+
declare readonly _buckets: HashCore<K, T[]>;
|
|
12
|
+
|
|
13
|
+
constructor(buckets: HashCore<K, T[]>) {
|
|
14
|
+
Object.defineProperty(this, "_buckets", { value: buckets });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The number of distinct keys. */
|
|
18
|
+
get count(): number {
|
|
19
|
+
return this._buckets.size;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The elements under `key`, in source order; an empty sequence when the key is absent. */
|
|
23
|
+
get(key: K): Iterable<T> {
|
|
24
|
+
return this._buckets.get(key) ?? [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
contains(key: K): boolean {
|
|
28
|
+
return this._buckets.has(key);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
[Symbol.iterator](): Iterator<Grouping<K, T>> {
|
|
32
|
+
return groupingsOf(this._buckets);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -13,8 +13,10 @@
|
|
|
13
13
|
* as their result is determined.
|
|
14
14
|
*/
|
|
15
15
|
import type { IEqualityComparer } from "../collections/default-comparer.ts";
|
|
16
|
+
import { HashCore } from "../collections/hash-core.ts";
|
|
16
17
|
import { HashSet } from "../collections/hash-set.ts";
|
|
17
18
|
import { compareKeys } from "./compare-keys.ts";
|
|
19
|
+
import { bucketsOf, groupingsOf } from "./grouping.ts";
|
|
18
20
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
19
21
|
import type {
|
|
20
22
|
AppendOp,
|
|
@@ -23,6 +25,7 @@ import type {
|
|
|
23
25
|
DistinctOp,
|
|
24
26
|
ExceptOp,
|
|
25
27
|
IntersectOp,
|
|
28
|
+
JoinOp,
|
|
26
29
|
OrderByDescendingOp,
|
|
27
30
|
OrderByOp,
|
|
28
31
|
PrependOp,
|
|
@@ -40,7 +43,6 @@ import type {
|
|
|
40
43
|
WhereOp,
|
|
41
44
|
ZipOp,
|
|
42
45
|
GroupByOp,
|
|
43
|
-
Grouping,
|
|
44
46
|
} from "./types.ts";
|
|
45
47
|
|
|
46
48
|
/**
|
|
@@ -454,35 +456,96 @@ export function thenByDescending<T, K>(
|
|
|
454
456
|
/**
|
|
455
457
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
456
458
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
457
|
-
* items in source order
|
|
458
|
-
*
|
|
459
|
+
* items in source order — in the order the keys are first seen. Keys are
|
|
460
|
+
* equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
|
|
461
|
+
* the source to bucket items but each enumeration of the result re-runs the bucketing.
|
|
459
462
|
*/
|
|
460
463
|
export function groupBy<T, K>(
|
|
461
464
|
keySelector: (item: T) => K,
|
|
465
|
+
comparer?: IEqualityComparer<NoInfer<K>> | null,
|
|
462
466
|
queryable?: QueryableMeta,
|
|
463
467
|
): GroupByOp<T, K> {
|
|
464
468
|
return {
|
|
465
469
|
kind: "groupBy",
|
|
466
470
|
keySelector,
|
|
471
|
+
comparer,
|
|
467
472
|
queryable,
|
|
468
473
|
apply: (source) => ({
|
|
474
|
+
// A generator, so the source is read on the first `next()`, as .NET's GroupBy does. With the
|
|
475
|
+
// default comparison the bucketing runs in this generator body, where V8 keeps it as fast as
|
|
476
|
+
// a native `Map` loop; a user comparer goes through the shared `bucketsOf`.
|
|
469
477
|
*[Symbol.iterator]() {
|
|
470
|
-
|
|
478
|
+
if (comparer) {
|
|
479
|
+
yield* groupingsOf(bucketsOf(source, keySelector, comparer));
|
|
480
|
+
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const buckets = new HashCore<K, T[]>();
|
|
485
|
+
const entries = buckets._entries;
|
|
486
|
+
|
|
471
487
|
for (const item of source) {
|
|
472
|
-
const
|
|
473
|
-
|
|
488
|
+
const key = keySelector(item);
|
|
489
|
+
|
|
490
|
+
if (typeof key === "object" && key !== null) {
|
|
491
|
+
buckets.append(key, item);
|
|
492
|
+
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const bucket = entries.get(key);
|
|
497
|
+
|
|
474
498
|
if (bucket === undefined) {
|
|
475
|
-
|
|
476
|
-
|
|
499
|
+
entries.set(key, [item]);
|
|
500
|
+
} else {
|
|
501
|
+
bucket.push(item);
|
|
477
502
|
}
|
|
478
|
-
bucket.push(item);
|
|
479
503
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
504
|
+
|
|
505
|
+
yield* groupingsOf(buckets);
|
|
506
|
+
},
|
|
507
|
+
}),
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
|
|
513
|
+
* outer element, in order, one result per inner element of an equal key, in inner order.
|
|
514
|
+
* A `null` key matches nothing. The inner source is read once the first outer element is, and an
|
|
515
|
+
* inner with no key ends the join there.
|
|
516
|
+
*/
|
|
517
|
+
export function join<T, U, K, R>(
|
|
518
|
+
inner: Iterable<U>,
|
|
519
|
+
outerKeySelector: (item: T) => K,
|
|
520
|
+
innerKeySelector: (item: U) => K,
|
|
521
|
+
resultSelector: (outer: T, inner: U) => R,
|
|
522
|
+
comparer?: IEqualityComparer<NoInfer<K>> | null,
|
|
523
|
+
): JoinOp<T, U, K, R> {
|
|
524
|
+
return {
|
|
525
|
+
kind: "join",
|
|
526
|
+
inner,
|
|
527
|
+
outerKeySelector,
|
|
528
|
+
innerKeySelector,
|
|
529
|
+
resultSelector,
|
|
530
|
+
comparer,
|
|
531
|
+
apply: (source) => ({
|
|
532
|
+
*[Symbol.iterator]() {
|
|
533
|
+
let matches: HashCore<K, U[]> | undefined;
|
|
534
|
+
|
|
535
|
+
for (const outer of source) {
|
|
536
|
+
if (matches === undefined) {
|
|
537
|
+
matches = bucketsOf(inner, innerKeySelector, comparer, undefined, true);
|
|
538
|
+
|
|
539
|
+
// .NET reads one outer element, and stops there when the inner has no key.
|
|
540
|
+
if (matches.size === 0) return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const key = outerKeySelector(outer);
|
|
544
|
+
const bucket = key === null || key === undefined ? undefined : matches.get(key);
|
|
545
|
+
|
|
546
|
+
if (bucket !== undefined) {
|
|
547
|
+
for (const item of bucket) yield resultSelector(outer, item);
|
|
548
|
+
}
|
|
486
549
|
}
|
|
487
550
|
},
|
|
488
551
|
}),
|