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.
- 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/comparable-compare-to.d.ts +12 -0
- package/dist/system/comparable-compare-to.js +20 -0
- 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/hash-code.d.ts +2 -1
- package/dist/system/hash-code.js +5 -2
- package/dist/system/index.d.ts +3 -0
- package/dist/system/index.js +3 -0
- package/dist/system/int64-math.js +2 -1
- 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/index.d.ts +1 -1
- package/dist/system/json/json-serializer.d.ts +12 -1
- package/dist/system/json/json-serializer.js +29 -0
- package/dist/system/json/json-text-writer.d.ts +24 -0
- package/dist/system/json/json-text-writer.js +207 -0
- package/dist/system/json/number-from-string.js +20 -6
- package/dist/system/json/numeric-converters.js +41 -0
- package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
- package/dist/system/json/ordinal-ignore-case.js +1 -1
- package/dist/system/json/serializer-context.d.ts +13 -7
- package/dist/system/json/serializer-context.js +23 -11
- package/dist/system/json/types.d.ts +20 -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 +27 -0
- package/dist/system/math.js +45 -0
- package/dist/system/min-max.d.ts +6 -0
- package/dist/system/min-max.js +10 -0
- package/dist/system/number-parsing.d.ts +16 -4
- package/dist/system/number-parsing.js +21 -4
- package/dist/system/string-comparison.d.ts +56 -0
- package/dist/system/string-comparison.js +225 -0
- package/dist/system/string-hash-code.d.ts +14 -0
- package/dist/system/string-hash-code.js +37 -0
- package/dist/system/string-members.d.ts +5 -4
- package/dist/system/string-members.js +12 -11
- 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/comparable-compare-to.d.ts +12 -0
- package/dist-unchecked/system/comparable-compare-to.js +20 -0
- 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/hash-code.d.ts +2 -1
- package/dist-unchecked/system/hash-code.js +5 -2
- package/dist-unchecked/system/index.d.ts +3 -0
- package/dist-unchecked/system/index.js +3 -0
- package/dist-unchecked/system/int64-math.js +2 -1
- 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/index.d.ts +1 -1
- package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
- package/dist-unchecked/system/json/json-serializer.js +29 -0
- package/dist-unchecked/system/json/json-text-writer.d.ts +24 -0
- package/dist-unchecked/system/json/json-text-writer.js +207 -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/json/ordinal-ignore-case.d.ts +2 -0
- package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
- package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
- package/dist-unchecked/system/json/serializer-context.js +23 -11
- package/dist-unchecked/system/json/types.d.ts +20 -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 +27 -0
- package/dist-unchecked/system/math.js +45 -0
- package/dist-unchecked/system/min-max.d.ts +6 -0
- package/dist-unchecked/system/min-max.js +10 -0
- package/dist-unchecked/system/number-parsing.d.ts +16 -4
- package/dist-unchecked/system/number-parsing.js +21 -4
- package/dist-unchecked/system/string-comparison.d.ts +56 -0
- package/dist-unchecked/system/string-comparison.js +225 -0
- package/dist-unchecked/system/string-hash-code.d.ts +14 -0
- package/dist-unchecked/system/string-hash-code.js +37 -0
- package/dist-unchecked/system/string-members.d.ts +5 -4
- package/dist-unchecked/system/string-members.js +12 -11
- 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/comparable-compare-to.ts +23 -0
- 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/hash-code.ts +7 -2
- package/src/system/index.ts +3 -0
- package/src/system/int64-math.ts +2 -1
- package/src/system/int64.ts +27 -10
- package/src/system/integer-division.ts +43 -6
- package/src/system/json/index.ts +1 -0
- package/src/system/json/json-serializer.ts +42 -0
- package/src/system/json/json-text-writer.ts +282 -0
- package/src/system/json/number-from-string.ts +26 -6
- package/src/system/json/numeric-converters.ts +55 -0
- package/src/system/json/ordinal-ignore-case.ts +1 -1
- package/src/system/json/serializer-context.ts +28 -11
- package/src/system/json/types.ts +21 -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 +63 -0
- package/src/system/min-max.ts +11 -0
- package/src/system/number-parsing.ts +24 -4
- package/src/system/string-comparison.ts +299 -0
- package/src/system/string-hash-code.ts +44 -0
- package/src/system/string-members.ts +12 -11
|
@@ -75,10 +75,10 @@ function isWithinRange(value, min, max) {
|
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Reads the floating-point grammar; `staysFinite` rejects a value that
|
|
78
|
-
* overflows the target type
|
|
79
|
-
*
|
|
78
|
+
* overflows the target type, and `round` brings the double to the target's
|
|
79
|
+
* precision (a `Single` rounds to single precision, #695).
|
|
80
80
|
*/
|
|
81
|
-
function floatingPointReader(staysFinite) {
|
|
81
|
+
function floatingPointReader(staysFinite, round) {
|
|
82
82
|
return (text) => {
|
|
83
83
|
const literal = FLOATING_POINT_LITERALS.get(text);
|
|
84
84
|
if (literal !== undefined) {
|
|
@@ -89,12 +89,26 @@ function floatingPointReader(staysFinite) {
|
|
|
89
89
|
return undefined;
|
|
90
90
|
}
|
|
91
91
|
const value = Number(text);
|
|
92
|
-
return staysFinite(parts, value) ? value : undefined;
|
|
92
|
+
return staysFinite(parts, value) ? round(value) : undefined;
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
95
|
function fitsDouble(_, value) {
|
|
96
96
|
return Number.isFinite(value);
|
|
97
97
|
}
|
|
98
|
+
/** The largest `Single`, `(2 − 2^-23) × 2^127`. */
|
|
99
|
+
const SINGLE_MAX_VALUE = 3.4028234663852886e38;
|
|
100
|
+
/**
|
|
101
|
+
* A finite double that {@link fitsSingle} accepted, rounded to single
|
|
102
|
+
* precision (#695). Only the threshold double itself would round to an
|
|
103
|
+
* infinity (a tie to even); its text was below the threshold, so it is
|
|
104
|
+
* `±Single.MaxValue`.
|
|
105
|
+
*/
|
|
106
|
+
function roundToFiniteSingle(value) {
|
|
107
|
+
const single = Math.fround(value);
|
|
108
|
+
return Number.isFinite(single) || !Number.isFinite(value)
|
|
109
|
+
? single
|
|
110
|
+
: Math.sign(single) * SINGLE_MAX_VALUE;
|
|
111
|
+
}
|
|
98
112
|
/**
|
|
99
113
|
* Whether the text stays below the `Single` overflow threshold. The double
|
|
100
114
|
* is already rounded, so it decides every case but one: a double equal to
|
|
@@ -173,8 +187,8 @@ const INTEGER_TOKEN_READERS = /* @__PURE__ */ lazyValue(() => integerReadersOf((
|
|
|
173
187
|
: SIGNED_INTEGER_TOKEN_PATTERN, () => (value) => value));
|
|
174
188
|
const NUMBER_TEXT_READERS = /* @__PURE__ */ lazyValue(() => ({
|
|
175
189
|
...STRICT_INTEGER_READERS(),
|
|
176
|
-
"System.Single": floatingPointReader(fitsSingle),
|
|
177
|
-
"System.Double": floatingPointReader(fitsDouble),
|
|
190
|
+
"System.Single": floatingPointReader(fitsSingle, roundToFiniteSingle),
|
|
191
|
+
"System.Double": floatingPointReader(fitsDouble, (value) => value),
|
|
178
192
|
}));
|
|
179
193
|
/**
|
|
180
194
|
* The number `text` spells as a `numberType`, or `undefined` when System.Text.Json
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { formatSingle } from "../double-format.js";
|
|
1
2
|
import { JsonException } from "./errors.js";
|
|
2
3
|
import { isDecimalText, readDecimalText } from "./decimal-text.js";
|
|
3
4
|
import { exactBigIntLeafOf, exactNumberTextOf, JsonNonIntegerNumber, numberOfTreeValue, writeExactNumber, } from "./exact-json-number.js";
|
|
@@ -27,6 +28,8 @@ const passthroughConverter = {
|
|
|
27
28
|
};
|
|
28
29
|
/** The CLR name of a C# `char`, which reads as a one-code-unit `string`. */
|
|
29
30
|
const CHAR_TYPE_NAME = "System.Char";
|
|
31
|
+
/** The CLR name of a C# `float`, whose values read rounded to single precision. */
|
|
32
|
+
const SINGLE_TYPE_NAME = "System.Single";
|
|
30
33
|
/**
|
|
31
34
|
* A `primitive`: values pass through both ways, except that a JSON string
|
|
32
35
|
* for a numeric `numberType` is read by System.Text.Json's grammar when the
|
|
@@ -39,6 +42,9 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
39
42
|
if (descriptor.typeName === CHAR_TYPE_NAME) {
|
|
40
43
|
return charConverterOf(errorTypeName);
|
|
41
44
|
}
|
|
45
|
+
if (numberType === SINGLE_TYPE_NAME) {
|
|
46
|
+
return singleConverterOf(readOptions, errorTypeName);
|
|
47
|
+
}
|
|
42
48
|
if (numberType === undefined || !readsNumbersFromStrings(readOptions)) {
|
|
43
49
|
return passthroughConverter;
|
|
44
50
|
}
|
|
@@ -56,6 +62,41 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
56
62
|
},
|
|
57
63
|
};
|
|
58
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* A `float`: a JSON number reads rounded to single precision, as
|
|
67
|
+
* System.Text.Json parses it into a `Single` (#695), and a string reads
|
|
68
|
+
* through the number grammar (which rounds too) when the context reads
|
|
69
|
+
* numbers from strings. A finite value writes the shortest text that reads
|
|
70
|
+
* back as the same `float`, as System.Text.Json writes it (`0.1`, not the
|
|
71
|
+
* double `0.10000000149011612`), through the exact-number leaf. A
|
|
72
|
+
* non-finite value passes through: System.Text.Json throws for it without
|
|
73
|
+
* `AllowNamedFloatingPointLiterals`, which Metano does not mirror.
|
|
74
|
+
*/
|
|
75
|
+
function singleConverterOf(readOptions, errorTypeName) {
|
|
76
|
+
const readsStrings = readsNumbersFromStrings(readOptions);
|
|
77
|
+
return {
|
|
78
|
+
serialize: (value) => typeof value === "number" && Number.isFinite(value)
|
|
79
|
+
? writeExactNumber(formatSingle(value), () => Math.fround(value))
|
|
80
|
+
: passthroughConverter.serialize(value),
|
|
81
|
+
deserialize: (value) => {
|
|
82
|
+
if (typeof value === "number") {
|
|
83
|
+
return Math.fround(value);
|
|
84
|
+
}
|
|
85
|
+
const exact = exactNumberTextOf(value);
|
|
86
|
+
if (exact !== undefined) {
|
|
87
|
+
return Math.fround(Number(exact));
|
|
88
|
+
}
|
|
89
|
+
if (typeof value !== "string" || !readsStrings) {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
const number = readNumberTextAsNumber(value, SINGLE_TYPE_NAME);
|
|
93
|
+
if (number === undefined) {
|
|
94
|
+
throw cannotConvert(SINGLE_TYPE_NAME, errorTypeName);
|
|
95
|
+
}
|
|
96
|
+
return number;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
59
100
|
/**
|
|
60
101
|
* A `long` / `ulong` — the `int64` descriptor (spec 012 FR-050), whatever
|
|
61
102
|
* the read options:
|
|
@@ -12,3 +12,5 @@
|
|
|
12
12
|
*/
|
|
13
13
|
/** The key under which `name` equals every name that matches it ignoring case. */
|
|
14
14
|
export declare function ordinalIgnoreCaseKeyOf(name: string): string;
|
|
15
|
+
/** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
|
|
16
|
+
export declare function ordinalUppercaseOf(character: string): string;
|
|
@@ -47,7 +47,7 @@ export function ordinalIgnoreCaseKeyOf(name) {
|
|
|
47
47
|
return Array.from(name, ordinalUppercaseOf).join("");
|
|
48
48
|
}
|
|
49
49
|
/** The ordinal uppercase of one code point (`character` may be a surrogate pair). */
|
|
50
|
-
function ordinalUppercaseOf(character) {
|
|
50
|
+
export function ordinalUppercaseOf(character) {
|
|
51
51
|
const exception = ORDINAL_UPPERCASE_EXCEPTIONS().get(character);
|
|
52
52
|
if (exception !== undefined) {
|
|
53
53
|
return exception;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TypeSpec, ValueSpec, SerializerContextOptions, JsonConverter, JsonReadOptions } from "./types.ts";
|
|
1
|
+
import type { TypeSpec, ValueSpec, SerializerContextOptions, JsonConverter, JsonReadOptions, JsonWriteOptions } from "./types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Base class for generated serializer contexts. Each context holds options
|
|
4
4
|
* (including custom converters) and a registry of type specs for lookup.
|
|
@@ -18,9 +18,15 @@ export declare abstract class SerializerContext {
|
|
|
18
18
|
* default.
|
|
19
19
|
*/
|
|
20
20
|
protected get readOptions(): JsonReadOptions;
|
|
21
|
+
/**
|
|
22
|
+
* The text formatting of every spec this context creates, mirrored from the
|
|
23
|
+
* C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
|
|
24
|
+
* #752). Generated contexts override it only for an indented context.
|
|
25
|
+
*/
|
|
26
|
+
protected get writeOptions(): JsonWriteOptions;
|
|
21
27
|
/**
|
|
22
28
|
* Registers a spec in the internal registry and returns it, with the
|
|
23
|
-
* context's read options attached. Intended to be called from lazy getters
|
|
29
|
+
* context's read and write options attached. Intended to be called from lazy getters
|
|
24
30
|
* in generated subclasses:
|
|
25
31
|
*
|
|
26
32
|
* ```ts
|
|
@@ -42,15 +48,15 @@ export declare abstract class SerializerContext {
|
|
|
42
48
|
* ```
|
|
43
49
|
*
|
|
44
50
|
* Nothing is registered: a value spec has no constructor to resolve. The
|
|
45
|
-
* context's read options are attached, as by `createSpec`.
|
|
51
|
+
* context's read and write options are attached, as by `createSpec`.
|
|
46
52
|
*/
|
|
47
53
|
protected createValueSpec<T>(spec: ValueSpec<T>): ValueSpec<T>;
|
|
48
54
|
/**
|
|
49
|
-
* `spec` with the context's read options, under any the spec
|
|
50
|
-
* itself. A spec stays as given when neither has one, so default
|
|
51
|
-
* keep their specs unchanged.
|
|
55
|
+
* `spec` with the context's read and write options, under any the spec
|
|
56
|
+
* declares itself. A spec stays as given when neither has one, so default
|
|
57
|
+
* contexts keep their specs unchanged.
|
|
52
58
|
*/
|
|
53
|
-
private
|
|
59
|
+
private withContextOptions;
|
|
54
60
|
/**
|
|
55
61
|
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
56
62
|
* was not registered via createSpec().
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/** System.Text.Json's default read behavior: no option set. */
|
|
2
2
|
const DEFAULT_READ_OPTIONS = /* @__PURE__ */ Object.freeze({});
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/** System.Text.Json's default text formatting: compact text. */
|
|
4
|
+
const DEFAULT_WRITE_OPTIONS = /* @__PURE__ */ Object.freeze({});
|
|
5
|
+
function hasNoOptions(options) {
|
|
6
|
+
return Object.keys(options).length === 0;
|
|
5
7
|
}
|
|
6
8
|
/**
|
|
7
9
|
* Base class for generated serializer contexts. Each context holds options
|
|
@@ -28,9 +30,17 @@ export class SerializerContext {
|
|
|
28
30
|
get readOptions() {
|
|
29
31
|
return DEFAULT_READ_OPTIONS;
|
|
30
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The text formatting of every spec this context creates, mirrored from the
|
|
35
|
+
* C# context's `[JsonSourceGenerationOptions]` (`WriteIndented` and friends,
|
|
36
|
+
* #752). Generated contexts override it only for an indented context.
|
|
37
|
+
*/
|
|
38
|
+
get writeOptions() {
|
|
39
|
+
return DEFAULT_WRITE_OPTIONS;
|
|
40
|
+
}
|
|
31
41
|
/**
|
|
32
42
|
* Registers a spec in the internal registry and returns it, with the
|
|
33
|
-
* context's read options attached. Intended to be called from lazy getters
|
|
43
|
+
* context's read and write options attached. Intended to be called from lazy getters
|
|
34
44
|
* in generated subclasses:
|
|
35
45
|
*
|
|
36
46
|
* ```ts
|
|
@@ -40,7 +50,7 @@ export class SerializerContext {
|
|
|
40
50
|
* ```
|
|
41
51
|
*/
|
|
42
52
|
createSpec(spec) {
|
|
43
|
-
const attached = this.
|
|
53
|
+
const attached = this.withContextOptions(spec);
|
|
44
54
|
// Only register class-backed types (those with a constructor reference).
|
|
45
55
|
// [PlainObject] specs have no `type` field — they're accessed via their
|
|
46
56
|
// getter on the context, not via constructor-keyed resolve().
|
|
@@ -61,19 +71,21 @@ export class SerializerContext {
|
|
|
61
71
|
* ```
|
|
62
72
|
*
|
|
63
73
|
* Nothing is registered: a value spec has no constructor to resolve. The
|
|
64
|
-
* context's read options are attached, as by `createSpec`.
|
|
74
|
+
* context's read and write options are attached, as by `createSpec`.
|
|
65
75
|
*/
|
|
66
76
|
createValueSpec(spec) {
|
|
67
|
-
return this.
|
|
77
|
+
return this.withContextOptions(spec);
|
|
68
78
|
}
|
|
69
79
|
/**
|
|
70
|
-
* `spec` with the context's read options, under any the spec
|
|
71
|
-
* itself. A spec stays as given when neither has one, so default
|
|
72
|
-
* keep their specs unchanged.
|
|
80
|
+
* `spec` with the context's read and write options, under any the spec
|
|
81
|
+
* declares itself. A spec stays as given when neither has one, so default
|
|
82
|
+
* contexts keep their specs unchanged.
|
|
73
83
|
*/
|
|
74
|
-
|
|
84
|
+
withContextOptions(spec) {
|
|
75
85
|
const readOptions = { ...this.readOptions, ...spec.readOptions };
|
|
76
|
-
|
|
86
|
+
const writeOptions = { ...this.writeOptions, ...spec.writeOptions };
|
|
87
|
+
const withReads = hasNoOptions(readOptions) ? spec : { ...spec, readOptions };
|
|
88
|
+
return hasNoOptions(writeOptions) ? withReads : { ...withReads, writeOptions };
|
|
77
89
|
}
|
|
78
90
|
/**
|
|
79
91
|
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
@@ -244,6 +244,8 @@ export interface TypeSpec<T = unknown> {
|
|
|
244
244
|
readonly typeName?: string;
|
|
245
245
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
246
246
|
readonly readOptions?: JsonReadOptions;
|
|
247
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
248
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
247
249
|
/**
|
|
248
250
|
* TS names of this contract's properties (its base chain's included) that
|
|
249
251
|
* a read sets after construction, and only when the payload carries their
|
|
@@ -309,6 +311,22 @@ export interface JsonReadOptions {
|
|
|
309
311
|
/** The type discriminator may appear after other properties (FR-002). */
|
|
310
312
|
readonly allowOutOfOrderMetadataProperties?: boolean;
|
|
311
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Context-level text formatting mirrored from `[JsonSourceGenerationOptions]`
|
|
316
|
+
* (`WriteIndented`, `IndentCharacter`, `IndentSize`, `NewLine`); only
|
|
317
|
+
* `serializeText` reads it. Every member is optional; an absent member is
|
|
318
|
+
* System.Text.Json's default.
|
|
319
|
+
*/
|
|
320
|
+
export interface JsonWriteOptions {
|
|
321
|
+
/** Write each element and property on its own line. */
|
|
322
|
+
readonly writeIndented?: boolean;
|
|
323
|
+
/** The indent character, a space (the default) or a tab. */
|
|
324
|
+
readonly indentCharacter?: " " | "\t";
|
|
325
|
+
/** The indent characters per nesting level, 0 to 127; 2 by default. */
|
|
326
|
+
readonly indentSize?: number;
|
|
327
|
+
/** The line break; `"\n"` by default (System.Text.Json's is the platform's). */
|
|
328
|
+
readonly newLine?: "\n" | "\r\n";
|
|
329
|
+
}
|
|
312
330
|
/**
|
|
313
331
|
* Spec of a non-object `[JsonSerializable]` root (a collection, dictionary,
|
|
314
332
|
* set, primitive, enum, branded or nullable type): the same descriptor that
|
|
@@ -337,6 +355,8 @@ export interface ValueSpec<T = unknown> {
|
|
|
337
355
|
readonly typeName?: string;
|
|
338
356
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
339
357
|
readonly readOptions?: JsonReadOptions;
|
|
358
|
+
/** The context's text formatting; absent when it writes compact text (the default). */
|
|
359
|
+
readonly writeOptions?: JsonWriteOptions;
|
|
340
360
|
}
|
|
341
361
|
/** Any spec the generated context exposes: an object contract or a value root. */
|
|
342
362
|
export type JsonTypeInfo<T = unknown> = TypeSpec<T> | ValueSpec<T>;
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
/** Elements of `source` bucketed by `keySelector`; a `null` key is skipped when `skipNullKeys`. */
|
|
10
|
+
export declare function bucketsOf<T, K, V = T>(source: Iterable<T>, keySelector: (item: T) => K, comparer: IEqualityComparer<K> | null | undefined, elementSelector?: (item: T) => V, skipNullKeys?: boolean): HashCore<K, V[]>;
|
|
11
|
+
/** The groupings of `buckets`: an iterable bucket that also reads `.key`. */
|
|
12
|
+
export declare function groupingsOf<K, T>(buckets: HashCore<K, T[]>): Generator<Grouping<K, T>>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { HashCore } from "../collections/hash-core.js";
|
|
2
|
+
/**
|
|
3
|
+
* `comparer` as .NET's `Lookup` uses it: a `null` key hashes to 0 without asking the
|
|
4
|
+
* comparer, which may throw on it (`GetHashCode(string value) => value.Length`).
|
|
5
|
+
*/
|
|
6
|
+
function nullSafe(comparer) {
|
|
7
|
+
return comparer === null || comparer === undefined
|
|
8
|
+
? comparer
|
|
9
|
+
: {
|
|
10
|
+
equals: (a, b) => comparer.equals(a, b),
|
|
11
|
+
getHashCode: (key) => key === null || key === undefined ? 0 : comparer.getHashCode(key),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Elements of `source` bucketed by `keySelector`; a `null` key is skipped when `skipNullKeys`. */
|
|
15
|
+
export function bucketsOf(source, keySelector, comparer, elementSelector, skipNullKeys = false) {
|
|
16
|
+
const buckets = new HashCore(nullSafe(comparer));
|
|
17
|
+
if (elementSelector === undefined && !skipNullKeys && comparer) {
|
|
18
|
+
return bucketAll(source, keySelector, buckets);
|
|
19
|
+
}
|
|
20
|
+
for (const item of source) {
|
|
21
|
+
const key = keySelector(item);
|
|
22
|
+
if (skipNullKeys && (key === null || key === undefined)) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
buckets.append(key, elementSelector === undefined ? item : elementSelector(item));
|
|
26
|
+
}
|
|
27
|
+
return buckets;
|
|
28
|
+
}
|
|
29
|
+
function bucketAll(source, keySelector, buckets) {
|
|
30
|
+
for (const item of source) {
|
|
31
|
+
buckets.append(keySelector(item), item);
|
|
32
|
+
}
|
|
33
|
+
return buckets;
|
|
34
|
+
}
|
|
35
|
+
/** The groupings of `buckets`: an iterable bucket that also reads `.key`. */
|
|
36
|
+
export function* groupingsOf(buckets) {
|
|
37
|
+
for (const [key, items] of buckets) {
|
|
38
|
+
// `Grouping` is `Iterable<T> & { key }`: the bucket array itself carries the key, so
|
|
39
|
+
// consumers can `for…of` the grouping and read `.key`.
|
|
40
|
+
yield Object.assign(items, { key });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -24,9 +24,10 @@ export type { Stage } from "./linq.ts";
|
|
|
24
24
|
export type { BinaryOp, ExprBinary, ExprCall, ExprCapture, ExprConditional, ExprConvert, ExprLambda, ExprLiteral, ExprMember, ExprNew, ExprParam, ExprTree, ExprTypeRef, ExprUnary, QueryableMeta, UnaryOp, } from "./expr-tree.ts";
|
|
25
25
|
export { ExprTreeVisitor, compareValues, compileLambdaBody, evaluateExprTree, readParamName, } from "./expr-tree-visitor.ts";
|
|
26
26
|
export type { ExprTreeScope } from "./expr-tree-visitor.ts";
|
|
27
|
-
export type { AnyOperator, AnyTerminal, OperatorBase, OperatorKind, TerminalBase, TerminalKind, AppendOp, ConcatOp, DistinctByOp, DistinctOp, ExceptOp, GroupByOp, Grouping, IntersectOp, OrderByDescendingOp, OrderByOp, ThenByDescendingOp, ThenByOp, PrependOp, ReverseOp, DefaultIfEmptyOp, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, UnionOp, WhereOp, ZipOp, AggregateTerm, AllTerm, AnyTerm, AverageTerm, AverageInt64Term, ContainsTerm, CountTerm, ElementAtTerm, ElementAtOrDefaultTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxTerm, MaxInt64Term, MaxByTerm, MinTerm, MinInt64Term, MinByTerm, SingleTerm, SingleOrDefaultTerm, SumTerm, SumInt64Term, ToArrayTerm, ToMapTerm, ToSetTerm, ToListTerm, ToDictionaryTerm, ToHashSetTerm, } from "./types.ts";
|
|
28
|
-
export { where, select, selectMany, take, skip, takeWhile, skipWhile, distinct, distinctBy, concat, union, intersect, except, append, prepend, reverse, orderBy, orderByDescending, thenBy, thenByDescending, groupBy, zip, defaultIfEmpty, } from "./operators.ts";
|
|
29
|
-
export { toArray, toMap, toSet, toList, toDictionary, toHashSet, first, firstOrDefault, last, lastOrDefault, single, singleOrDefault, any, all, count, sum, min, minNullable, max, maxNullable, minBy, minByNullable, maxBy, maxByNullable, average, averageNullable, contains, aggregate, elementAt, elementAtOrDefault, } from "./terminals.ts";
|
|
27
|
+
export type { AnyOperator, AnyTerminal, OperatorBase, OperatorKind, TerminalBase, TerminalKind, AppendOp, ConcatOp, DistinctByOp, DistinctOp, ExceptOp, GroupByOp, Grouping, IntersectOp, OrderByDescendingOp, OrderByOp, ThenByDescendingOp, ThenByOp, PrependOp, ReverseOp, DefaultIfEmptyOp, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, UnionOp, WhereOp, ZipOp, AggregateTerm, AllTerm, AnyTerm, AverageTerm, AverageInt64Term, ContainsTerm, CountTerm, ElementAtTerm, ElementAtOrDefaultTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxTerm, MaxInt64Term, MaxByTerm, MinTerm, MinInt64Term, MinByTerm, SingleTerm, SingleOrDefaultTerm, SumTerm, SumInt64Term, ToArrayTerm, ToMapTerm, ToSetTerm, ToListTerm, ToDictionaryTerm, ToHashSetTerm, ToLookupTerm, SequenceEqualTerm, JoinOp, } from "./types.ts";
|
|
28
|
+
export { where, select, selectMany, take, skip, takeWhile, skipWhile, distinct, distinctBy, concat, union, intersect, except, append, prepend, reverse, orderBy, orderByDescending, thenBy, thenByDescending, groupBy, join, zip, defaultIfEmpty, } from "./operators.ts";
|
|
29
|
+
export { toArray, toMap, toSet, toList, toDictionary, toHashSet, toLookup, sequenceEqual, first, firstOrDefault, last, lastOrDefault, single, singleOrDefault, any, all, count, sum, min, minNullable, max, maxNullable, minBy, minByNullable, maxBy, maxByNullable, average, averageNullable, contains, aggregate, elementAt, elementAtOrDefault, } from "./terminals.ts";
|
|
30
30
|
export { averageInt64, averageNullableInt64, maxInt64Of, maxNullableInt64Of, maxUInt64Of, minInt64Of, minNullableInt64Of, minUInt64Of, sumInt64, } from "./int64-terminals.ts";
|
|
31
|
+
export { Lookup } from "./lookup.ts";
|
|
31
32
|
export { averageDecimal, averageNullableDecimal, sumDecimal } from "./decimal-terminals.ts";
|
|
32
33
|
export type { DecimalConstructor, DecimalValue } from "./decimal-terminals.ts";
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
*/
|
|
22
22
|
export { LINQ_STAGES, getStages, linq, stages } from "./linq.js";
|
|
23
23
|
export { ExprTreeVisitor, compareValues, compileLambdaBody, evaluateExprTree, readParamName, } from "./expr-tree-visitor.js";
|
|
24
|
-
export { where, select, selectMany, take, skip, takeWhile, skipWhile, distinct, distinctBy, concat, union, intersect, except, append, prepend, reverse, orderBy, orderByDescending, thenBy, thenByDescending, groupBy, zip, defaultIfEmpty, } from "./operators.js";
|
|
25
|
-
export { toArray, toMap, toSet, toList, toDictionary, toHashSet, first, firstOrDefault, last, lastOrDefault, single, singleOrDefault, any, all, count, sum, min, minNullable, max, maxNullable, minBy, minByNullable, maxBy, maxByNullable, average, averageNullable, contains, aggregate, elementAt, elementAtOrDefault, } from "./terminals.js";
|
|
24
|
+
export { where, select, selectMany, take, skip, takeWhile, skipWhile, distinct, distinctBy, concat, union, intersect, except, append, prepend, reverse, orderBy, orderByDescending, thenBy, thenByDescending, groupBy, join, zip, defaultIfEmpty, } from "./operators.js";
|
|
25
|
+
export { toArray, toMap, toSet, toList, toDictionary, toHashSet, toLookup, sequenceEqual, first, firstOrDefault, last, lastOrDefault, single, singleOrDefault, any, all, count, sum, min, minNullable, max, maxNullable, minBy, minByNullable, maxBy, maxByNullable, average, averageNullable, contains, aggregate, elementAt, elementAtOrDefault, } from "./terminals.js";
|
|
26
26
|
export { averageInt64, averageNullableInt64, maxInt64Of, maxNullableInt64Of, maxUInt64Of, minInt64Of, minNullableInt64Of, minUInt64Of, sumInt64, } from "./int64-terminals.js";
|
|
27
|
+
export { Lookup } from "./lookup.js";
|
|
27
28
|
export { averageDecimal, averageNullableDecimal, sumDecimal } from "./decimal-terminals.js";
|
package/dist/system/linq/linq.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export function linq(source, ...ops) {
|
|
2
|
-
const
|
|
2
|
+
const start = typeof source === "string" ? codeUnitsOf(source) : source;
|
|
3
|
+
const result = ops.reduce((acc, op) => op.apply(acc), start);
|
|
3
4
|
// Attach the original stage list to the materialized result so an
|
|
4
5
|
// IQueryable-style consumer can introspect the chain. Skipped for
|
|
5
6
|
// primitive results (terminals like `count` / `sum` return numbers
|
|
@@ -44,3 +45,21 @@ export function stages(...ops) {
|
|
|
44
45
|
return ops;
|
|
45
46
|
}
|
|
46
47
|
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
48
|
+
/**
|
|
49
|
+
* The UTF-16 code units of `text`, read lazily: a C# string enumerates them,
|
|
50
|
+
* where a JS string's own iterator yields code points, so a surrogate pair
|
|
51
|
+
* would be one element (#695). No array is built, so an early-exit operator
|
|
52
|
+
* (`first`, `any`, `take`) reads only the units it needs.
|
|
53
|
+
*/
|
|
54
|
+
function codeUnitsOf(text) {
|
|
55
|
+
return {
|
|
56
|
+
[Symbol.iterator]: () => {
|
|
57
|
+
let index = 0;
|
|
58
|
+
return {
|
|
59
|
+
next: () => index < text.length
|
|
60
|
+
? { done: false, value: text[index++] }
|
|
61
|
+
: { done: true, value: undefined },
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 type { Grouping } from "./types.ts";
|
|
8
|
+
export declare class Lookup<K, T> implements Iterable<Grouping<K, T>> {
|
|
9
|
+
readonly _buckets: HashCore<K, T[]>;
|
|
10
|
+
constructor(buckets: HashCore<K, T[]>);
|
|
11
|
+
/** The number of distinct keys. */
|
|
12
|
+
get count(): number;
|
|
13
|
+
/** The elements under `key`, in source order; an empty sequence when the key is absent. */
|
|
14
|
+
get(key: K): Iterable<T>;
|
|
15
|
+
contains(key: K): boolean;
|
|
16
|
+
[Symbol.iterator](): Iterator<Grouping<K, T>>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { groupingsOf } from "./grouping.js";
|
|
2
|
+
export class Lookup {
|
|
3
|
+
constructor(buckets) {
|
|
4
|
+
Object.defineProperty(this, "_buckets", { value: buckets });
|
|
5
|
+
}
|
|
6
|
+
/** The number of distinct keys. */
|
|
7
|
+
get count() {
|
|
8
|
+
return this._buckets.size;
|
|
9
|
+
}
|
|
10
|
+
/** The elements under `key`, in source order; an empty sequence when the key is absent. */
|
|
11
|
+
get(key) {
|
|
12
|
+
return this._buckets.get(key) ?? [];
|
|
13
|
+
}
|
|
14
|
+
contains(key) {
|
|
15
|
+
return this._buckets.has(key);
|
|
16
|
+
}
|
|
17
|
+
[Symbol.iterator]() {
|
|
18
|
+
return groupingsOf(this._buckets);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import type { IEqualityComparer } from "../collections/default-comparer.ts";
|
|
16
16
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
17
|
-
import type { AppendOp, ConcatOp, DistinctByOp, DistinctOp, ExceptOp, IntersectOp, OrderByDescendingOp, OrderByOp, PrependOp, ReverseOp, DefaultIfEmptyOp, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, UnionOp, ThenByDescendingOp, ThenByOp, WhereOp, ZipOp, GroupByOp } from "./types.ts";
|
|
17
|
+
import type { AppendOp, ConcatOp, DistinctByOp, DistinctOp, ExceptOp, IntersectOp, JoinOp, OrderByDescendingOp, OrderByOp, PrependOp, ReverseOp, DefaultIfEmptyOp, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, UnionOp, ThenByDescendingOp, ThenByOp, WhereOp, ZipOp, GroupByOp } from "./types.ts";
|
|
18
18
|
export declare function where<T>(predicate: (item: T, index: number) => boolean, queryable?: QueryableMeta): WhereOp<T>;
|
|
19
19
|
export declare function select<T, R>(selector: (item: T, index: number) => R, queryable?: QueryableMeta): SelectOp<T, R>;
|
|
20
20
|
export declare function selectMany<T, R>(selector: (item: T, index: number) => Iterable<R>, queryable?: QueryableMeta): SelectManyOp<T, R>;
|
|
@@ -55,8 +55,16 @@ export declare function thenByDescending<T, K>(keySelector: (item: T) => K, quer
|
|
|
55
55
|
/**
|
|
56
56
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
57
57
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
58
|
-
* items in source order
|
|
59
|
-
*
|
|
58
|
+
* items in source order — in the order the keys are first seen. Keys are
|
|
59
|
+
* equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
|
|
60
|
+
* the source to bucket items but each enumeration of the result re-runs the bucketing.
|
|
60
61
|
*/
|
|
61
|
-
export declare function groupBy<T, K>(keySelector: (item: T) => K, queryable?: QueryableMeta): GroupByOp<T, K>;
|
|
62
|
+
export declare function groupBy<T, K>(keySelector: (item: T) => K, comparer?: IEqualityComparer<NoInfer<K>> | null, queryable?: QueryableMeta): GroupByOp<T, K>;
|
|
63
|
+
/**
|
|
64
|
+
* `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
|
|
65
|
+
* outer element, in order, one result per inner element of an equal key, in inner order.
|
|
66
|
+
* A `null` key matches nothing. The inner source is read once the first outer element is, and an
|
|
67
|
+
* inner with no key ends the join there.
|
|
68
|
+
*/
|
|
69
|
+
export declare function join<T, U, K, R>(inner: Iterable<U>, outerKeySelector: (item: T) => K, innerKeySelector: (item: U) => K, resultSelector: (outer: T, inner: U) => R, comparer?: IEqualityComparer<NoInfer<K>> | null): JoinOp<T, U, K, R>;
|
|
62
70
|
export declare function zip<T, U, R>(other: Iterable<U>, resultSelector: (first: T, second: U) => R, queryable?: QueryableMeta): ZipOp<T, U, R>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { HashCore } from "../collections/hash-core.js";
|
|
1
2
|
import { HashSet } from "../collections/hash-set.js";
|
|
2
3
|
import { compareKeys } from "./compare-keys.js";
|
|
4
|
+
import { bucketsOf, groupingsOf } from "./grouping.js";
|
|
3
5
|
class OrderedIter {
|
|
4
6
|
source;
|
|
5
7
|
comparers;
|
|
@@ -355,32 +357,76 @@ export function thenByDescending(keySelector, queryable) {
|
|
|
355
357
|
/**
|
|
356
358
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
357
359
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
358
|
-
* items in source order
|
|
359
|
-
*
|
|
360
|
+
* items in source order — in the order the keys are first seen. Keys are
|
|
361
|
+
* equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
|
|
362
|
+
* the source to bucket items but each enumeration of the result re-runs the bucketing.
|
|
360
363
|
*/
|
|
361
|
-
export function groupBy(keySelector, queryable) {
|
|
364
|
+
export function groupBy(keySelector, comparer, queryable) {
|
|
362
365
|
return {
|
|
363
366
|
kind: "groupBy",
|
|
364
367
|
keySelector,
|
|
368
|
+
comparer,
|
|
365
369
|
queryable,
|
|
366
370
|
apply: (source) => ({
|
|
371
|
+
// A generator, so the source is read on the first `next()`, as .NET's GroupBy does. With the
|
|
372
|
+
// default comparison the bucketing runs in this generator body, where V8 keeps it as fast as
|
|
373
|
+
// a native `Map` loop; a user comparer goes through the shared `bucketsOf`.
|
|
367
374
|
*[Symbol.iterator]() {
|
|
368
|
-
|
|
375
|
+
if (comparer) {
|
|
376
|
+
yield* groupingsOf(bucketsOf(source, keySelector, comparer));
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const buckets = new HashCore();
|
|
380
|
+
const entries = buckets._entries;
|
|
369
381
|
for (const item of source) {
|
|
370
|
-
const
|
|
371
|
-
|
|
382
|
+
const key = keySelector(item);
|
|
383
|
+
if (typeof key === "object" && key !== null) {
|
|
384
|
+
buckets.append(key, item);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const bucket = entries.get(key);
|
|
372
388
|
if (bucket === undefined) {
|
|
373
|
-
|
|
374
|
-
|
|
389
|
+
entries.set(key, [item]);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
bucket.push(item);
|
|
375
393
|
}
|
|
376
|
-
bucket.push(item);
|
|
377
394
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
395
|
+
yield* groupingsOf(buckets);
|
|
396
|
+
},
|
|
397
|
+
}),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
|
|
402
|
+
* outer element, in order, one result per inner element of an equal key, in inner order.
|
|
403
|
+
* A `null` key matches nothing. The inner source is read once the first outer element is, and an
|
|
404
|
+
* inner with no key ends the join there.
|
|
405
|
+
*/
|
|
406
|
+
export function join(inner, outerKeySelector, innerKeySelector, resultSelector, comparer) {
|
|
407
|
+
return {
|
|
408
|
+
kind: "join",
|
|
409
|
+
inner,
|
|
410
|
+
outerKeySelector,
|
|
411
|
+
innerKeySelector,
|
|
412
|
+
resultSelector,
|
|
413
|
+
comparer,
|
|
414
|
+
apply: (source) => ({
|
|
415
|
+
*[Symbol.iterator]() {
|
|
416
|
+
let matches;
|
|
417
|
+
for (const outer of source) {
|
|
418
|
+
if (matches === undefined) {
|
|
419
|
+
matches = bucketsOf(inner, innerKeySelector, comparer, undefined, true);
|
|
420
|
+
// .NET reads one outer element, and stops there when the inner has no key.
|
|
421
|
+
if (matches.size === 0)
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const key = outerKeySelector(outer);
|
|
425
|
+
const bucket = key === null || key === undefined ? undefined : matches.get(key);
|
|
426
|
+
if (bucket !== undefined) {
|
|
427
|
+
for (const item of bucket)
|
|
428
|
+
yield resultSelector(outer, item);
|
|
429
|
+
}
|
|
384
430
|
}
|
|
385
431
|
},
|
|
386
432
|
}),
|
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal factories — each returns a tagged descriptor whose `apply`
|
|
3
|
+
* walks the source and produces a concrete value (array, scalar,
|
|
4
|
+
* boolean). Same descriptor shape as composition operators so an
|
|
5
|
+
* IQueryable provider can switch over the terminal kind exhaustively
|
|
6
|
+
* (e.g. translate `count` to `SELECT COUNT(*)`, `sum` to
|
|
7
|
+
* `SELECT SUM(<selector>)`, etc.).
|
|
8
|
+
*
|
|
9
|
+
* Short-circuit terminals (`first`, `any`, `contains`) stop iterating
|
|
10
|
+
* as soon as the answer is known.
|
|
11
|
+
*/
|
|
12
|
+
import { type IEqualityComparer } from "../collections/default-comparer.ts";
|
|
1
13
|
import type { RuntimeType } from "../reflection/runtime-type.ts";
|
|
2
14
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
3
|
-
import type { AggregateTerm, AllTerm, AnyTerm, AverageTerm, ContainsTerm, CountTerm, ElementAtOrDefaultTerm, ElementAtTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxByTerm, MaxTerm, MinByTerm, MinTerm, SingleOrDefaultTerm, SingleTerm, SumTerm, ToArrayTerm, ToDictionaryTerm, ToHashSetTerm, ToListTerm, ToMapTerm, ToSetTerm } from "./types.ts";
|
|
15
|
+
import type { AggregateTerm, AllTerm, AnyTerm, AverageTerm, ContainsTerm, CountTerm, ElementAtOrDefaultTerm, ElementAtTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxByTerm, MaxTerm, MinByTerm, MinTerm, SingleOrDefaultTerm, SingleTerm, SequenceEqualTerm, SumTerm, ToArrayTerm, ToDictionaryTerm, ToHashSetTerm, ToListTerm, ToLookupTerm, ToMapTerm, ToSetTerm } from "./types.ts";
|
|
4
16
|
export declare function toArray<T>(): ToArrayTerm<T>;
|
|
5
17
|
export declare function toMap<T, K, V = T>(keySelector: (item: T) => K, valueSelector?: (item: T) => V, keyQueryable?: QueryableMeta, valueQueryable?: QueryableMeta): ToMapTerm<T, K, V>;
|
|
6
18
|
export declare function toSet<T>(): ToSetTerm<T>;
|
|
@@ -108,5 +120,20 @@ export declare function maxByNullable<T, K>(keySelector: (item: T) => K, queryab
|
|
|
108
120
|
export declare function average<T>(selector?: (item: T) => number, queryable?: QueryableMeta): AverageTerm<T>;
|
|
109
121
|
/** `Average()` / `Average(selector)` over `int?`, `double?`, …: nulls skipped; `null` when none is left. */
|
|
110
122
|
export declare function averageNullable<T>(selector?: (item: T) => number | null, queryable?: QueryableMeta): AverageTerm<T, number | null>;
|
|
111
|
-
|
|
123
|
+
/**
|
|
124
|
+
* `Contains(value[, comparer])`: whether an element equals `value` by `comparer`, or, without
|
|
125
|
+
* one, by .NET's `EqualityComparer<T>.Default` (a record, `NaN` and a decimal by value).
|
|
126
|
+
*/
|
|
127
|
+
export declare function contains<T>(value: T, comparer?: IEqualityComparer<NoInfer<T>> | null): ContainsTerm<T>;
|
|
128
|
+
/**
|
|
129
|
+
* `SequenceEqual(second[, comparer])`: whether both sequences have the same length and equal
|
|
130
|
+
* elements in order, equal by `comparer` or by `EqualityComparer<T>.Default`.
|
|
131
|
+
*/
|
|
132
|
+
export declare function sequenceEqual<T>(second: Iterable<T>, comparer?: IEqualityComparer<NoInfer<T>> | null): SequenceEqualTerm<T>;
|
|
133
|
+
/**
|
|
134
|
+
* `ToLookup(keySelector[, elementSelector][, comparer])`: a `Lookup` of the elements by key.
|
|
135
|
+
* The second argument is the `elementSelector` function or, in the `(keySelector, comparer)`
|
|
136
|
+
* overload, the comparer object.
|
|
137
|
+
*/
|
|
138
|
+
export declare function toLookup<T, K, V = T>(keySelector: (item: T) => K, elementSelector?: ((item: T) => V) | IEqualityComparer<NoInfer<K>> | null, comparer?: IEqualityComparer<NoInfer<K>> | null): ToLookupTerm<T, K, V>;
|
|
112
139
|
export declare function aggregate<T, A>(seed: A, accumulator: (acc: A, item: T) => A): AggregateTerm<T, A>;
|