metano-runtime 3.0.1 → 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 +51 -0
- package/dist/system/char-members.js +108 -0
- package/dist/system/collections/array-helpers.d.ts +28 -0
- package/dist/system/collections/array-helpers.js +105 -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/index.d.ts +2 -0
- package/dist/system/collections/index.js +2 -0
- package/dist/system/collections/interfaces.d.ts +13 -0
- package/dist/system/collections/list.d.ts +3 -0
- package/dist/system/collections/list.js +8 -11
- 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/collections/string-comparer.d.ts +14 -0
- package/dist/system/collections/string-comparer.js +45 -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/index.d.ts +5 -0
- package/dist/system/index.js +5 -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/contract-name.d.ts +2 -1
- package/dist/system/json/contract-name.js +3 -2
- package/dist/system/json/converters.d.ts +15 -52
- package/dist/system/json/converters.js +33 -317
- package/dist/system/json/dictionary-keys.js +1 -3
- package/dist/system/json/index.d.ts +1 -1
- package/dist/system/json/index.js +1 -1
- package/dist/system/json/json-serializer.d.ts +12 -1
- package/dist/system/json/json-serializer.js +29 -1
- 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/null-reads.d.ts +2 -1
- package/dist/system/json/null-reads.js +3 -1
- package/dist/system/json/number-from-string.d.ts +2 -0
- package/dist/system/json/number-from-string.js +24 -6
- package/dist/system/json/numeric-converters.d.ts +4 -4
- package/dist/system/json/numeric-converters.js +46 -8
- package/dist/system/json/numeric-enum-members.d.ts +3 -3
- package/dist/system/json/numeric-enum-members.js +2 -4
- package/dist/system/json/spec-plans.d.ts +102 -0
- package/dist/system/json/spec-plans.js +312 -0
- package/dist/system/json/types.d.ts +15 -9
- package/dist/system/lazy.d.ts +15 -0
- package/dist/system/lazy.js +43 -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 +35 -5
- package/dist/system/linq/operators.js +82 -31
- package/dist/system/linq/terminals.d.ts +41 -2
- package/dist/system/linq/terminals.js +122 -2
- package/dist/system/linq/types.d.ts +48 -4
- package/dist/system/math.d.ts +7 -0
- package/dist/system/math.js +14 -0
- package/dist/system/number-members.d.ts +10 -0
- package/dist/system/number-members.js +25 -0
- package/dist/system/number-parsing.d.ts +16 -4
- package/dist/system/number-parsing.js +21 -4
- package/dist/system/string-builder.d.ts +30 -0
- package/dist/system/string-builder.js +86 -0
- package/dist/system/task-completion-source.d.ts +37 -0
- package/dist/system/task-completion-source.js +81 -0
- package/dist-unchecked/system/char-members.d.ts +51 -0
- package/dist-unchecked/system/char-members.js +108 -0
- package/dist-unchecked/system/collections/array-helpers.d.ts +28 -0
- package/dist-unchecked/system/collections/array-helpers.js +105 -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/index.d.ts +2 -0
- package/dist-unchecked/system/collections/index.js +2 -0
- 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 +8 -11
- 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/collections/string-comparer.d.ts +14 -0
- package/dist-unchecked/system/collections/string-comparer.js +45 -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/index.d.ts +5 -0
- package/dist-unchecked/system/index.js +5 -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/contract-name.d.ts +2 -1
- package/dist-unchecked/system/json/contract-name.js +3 -2
- package/dist-unchecked/system/json/converters.d.ts +15 -52
- package/dist-unchecked/system/json/converters.js +33 -317
- package/dist-unchecked/system/json/dictionary-keys.js +1 -3
- package/dist-unchecked/system/json/index.d.ts +1 -1
- package/dist-unchecked/system/json/index.js +1 -1
- package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
- package/dist-unchecked/system/json/json-serializer.js +29 -1
- 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/null-reads.d.ts +2 -1
- package/dist-unchecked/system/json/null-reads.js +3 -1
- package/dist-unchecked/system/json/number-from-string.d.ts +2 -0
- package/dist-unchecked/system/json/number-from-string.js +24 -6
- package/dist-unchecked/system/json/numeric-converters.d.ts +4 -4
- package/dist-unchecked/system/json/numeric-converters.js +46 -8
- package/dist-unchecked/system/json/numeric-enum-members.d.ts +3 -3
- package/dist-unchecked/system/json/numeric-enum-members.js +2 -4
- package/dist-unchecked/system/json/spec-plans.d.ts +102 -0
- package/dist-unchecked/system/json/spec-plans.js +312 -0
- package/dist-unchecked/system/json/types.d.ts +15 -9
- package/dist-unchecked/system/lazy.d.ts +15 -0
- package/dist-unchecked/system/lazy.js +43 -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 +35 -5
- package/dist-unchecked/system/linq/operators.js +82 -31
- package/dist-unchecked/system/linq/terminals.d.ts +41 -2
- package/dist-unchecked/system/linq/terminals.js +122 -2
- package/dist-unchecked/system/linq/types.d.ts +48 -4
- package/dist-unchecked/system/math.d.ts +7 -0
- package/dist-unchecked/system/math.js +14 -0
- package/dist-unchecked/system/number-members.d.ts +10 -0
- package/dist-unchecked/system/number-members.js +25 -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-builder.d.ts +30 -0
- package/dist-unchecked/system/string-builder.js +86 -0
- package/dist-unchecked/system/task-completion-source.d.ts +37 -0
- package/dist-unchecked/system/task-completion-source.js +81 -0
- package/package.json +1 -1
- package/src/system/char-members.ts +131 -0
- package/src/system/collections/array-helpers.ts +139 -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/index.ts +2 -0
- package/src/system/collections/interfaces.ts +16 -0
- package/src/system/collections/list.ts +10 -15
- package/src/system/collections/read-only-list.ts +35 -1
- package/src/system/collections/sorted-list.ts +15 -1
- package/src/system/collections/string-comparer.ts +57 -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/index.ts +5 -0
- package/src/system/int64.ts +27 -10
- package/src/system/integer-division.ts +43 -6
- package/src/system/json/contract-name.ts +3 -2
- package/src/system/json/converters.ts +38 -526
- package/src/system/json/dictionary-keys.ts +6 -4
- package/src/system/json/index.ts +1 -1
- package/src/system/json/json-serializer.ts +42 -1
- package/src/system/json/json-text-writer.ts +224 -0
- package/src/system/json/null-reads.ts +3 -1
- package/src/system/json/number-from-string.ts +31 -6
- package/src/system/json/numeric-converters.ts +60 -8
- package/src/system/json/numeric-enum-members.ts +4 -6
- package/src/system/json/spec-plans.ts +524 -0
- package/src/system/json/types.ts +15 -9
- package/src/system/lazy.ts +62 -0
- package/src/system/linq/grouping.ts +77 -0
- package/src/system/linq/index.ts +14 -0
- package/src/system/linq/linq.ts +23 -1
- package/src/system/linq/lookup.ts +34 -0
- package/src/system/linq/operators.ts +109 -17
- package/src/system/linq/terminals.ts +161 -2
- package/src/system/linq/types.ts +66 -3
- package/src/system/math.ts +19 -0
- package/src/system/number-members.ts +29 -0
- package/src/system/number-parsing.ts +24 -4
- package/src/system/string-builder.ts +117 -0
- package/src/system/task-completion-source.ts +106 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// Only the contract walks (the hot path: one container per object) open and
|
|
2
|
+
// close containers with enter/exit; the collection converters keep the
|
|
3
|
+
// closure-based `readContainer` / `writeContainer` (#456).
|
|
4
|
+
import { enterReadContainer, enterWriteContainer, exitContainer } from "./container-depth.js";
|
|
5
|
+
import { cannotConvertTo, contractNameOf } from "./contract-name.js";
|
|
6
|
+
import { valueTypeNameOf } from "./null-reads.js";
|
|
7
|
+
import { createConverterSetCache } from "./converter-set-cache.js";
|
|
8
|
+
import { JsonException } from "./errors.js";
|
|
9
|
+
import { numberOfTreeValue } from "./exact-json-number.js";
|
|
10
|
+
import { hasTaggedRegistration, resolveDeserializeTarget, resolveSerializeTarget, } from "./polymorphism.js";
|
|
11
|
+
import { propertyLookupPlanOf } from "./property-lookup.js";
|
|
12
|
+
import { resolveConverter } from "./converters.js";
|
|
13
|
+
// ─── Core serialize/deserialize by spec ──────────────────────────────────────
|
|
14
|
+
/**
|
|
15
|
+
* Collects all properties from a spec chain, walking the base hierarchy.
|
|
16
|
+
* Base properties come first, own properties last: the declaration order a
|
|
17
|
+
* factory reads, **not** the wire order (see `collectWireProperties`). An
|
|
18
|
+
* override appears once per spec that declares it.
|
|
19
|
+
*/
|
|
20
|
+
export function collectProperties(spec) {
|
|
21
|
+
const base = spec.base ? collectProperties(spec.base) : [];
|
|
22
|
+
return [...base, ...spec.properties];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The properties a spec writes and reads, in System.Text.Json's wire order
|
|
26
|
+
* (FR-009): the spec's own properties, then its base's, recursively. A JSON
|
|
27
|
+
* name appears once, from its most-derived declaration, and a base
|
|
28
|
+
* declaration whose `ts` name a more-derived spec lists in
|
|
29
|
+
* `ignoredProperties` (a `[JsonIgnore]` override) is left out. When any
|
|
30
|
+
* property has an `order` (`[JsonPropertyOrder]`), the list is then sorted by
|
|
31
|
+
* it, stably, across the whole chain (measured: `{"Der2":20,"Base3":3,"Der1":10,
|
|
32
|
+
* "Base1":1,"Der3":30,"Base2":2}` for orders -5, -5, 0, 0, 1, 5).
|
|
33
|
+
*/
|
|
34
|
+
export function collectWireProperties(spec) {
|
|
35
|
+
const wire = [];
|
|
36
|
+
const writtenNames = new Set();
|
|
37
|
+
const ignoredNames = new Set();
|
|
38
|
+
for (const contract of specChainOf(spec)) {
|
|
39
|
+
const visible = contract.properties.filter((property) => !ignoredNames.has(property.ts) && !writtenNames.has(property.json));
|
|
40
|
+
for (const property of visible) {
|
|
41
|
+
writtenNames.add(property.json);
|
|
42
|
+
wire.push(property);
|
|
43
|
+
}
|
|
44
|
+
for (const name of contract.ignoredProperties ?? []) {
|
|
45
|
+
ignoredNames.add(name);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return wire.some(hasExplicitOrder) ? wire.sort(byPropertyOrder) : wire;
|
|
49
|
+
}
|
|
50
|
+
function hasExplicitOrder(property) {
|
|
51
|
+
return property.order !== undefined;
|
|
52
|
+
}
|
|
53
|
+
function byPropertyOrder(left, right) {
|
|
54
|
+
return (left.order ?? 0) - (right.order ?? 0);
|
|
55
|
+
}
|
|
56
|
+
/** The spec and its bases, most-derived first. */
|
|
57
|
+
function* specChainOf(spec) {
|
|
58
|
+
yield spec;
|
|
59
|
+
if (spec.base) {
|
|
60
|
+
yield* specChainOf(spec.base);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The plans of every custom-converter set, per kind of tree numbers. A plan
|
|
65
|
+
* holds converters resolved against its set, so the set is part of the key:
|
|
66
|
+
* plans are cached per spec inside each set's entry
|
|
67
|
+
* (`converter-set-cache.ts`). Only `deserializeText` reads exact trees, so
|
|
68
|
+
* the `exact` plans exist only for the specs it reads that way.
|
|
69
|
+
*/
|
|
70
|
+
const specPlansByTreeNumbers = {
|
|
71
|
+
parsed: /* @__PURE__ */ createConverterSetCache((converters) => createSpecPlans(converters, "parsed")),
|
|
72
|
+
exact: /* @__PURE__ */ createConverterSetCache((converters) => createSpecPlans(converters, "exact")),
|
|
73
|
+
};
|
|
74
|
+
function specPlansFor(customConverters, treeNumbers = "parsed") {
|
|
75
|
+
return specPlansByTreeNumbers[treeNumbers].entryFor(customConverters);
|
|
76
|
+
}
|
|
77
|
+
function createSpecPlans(customConverters, treeNumbers) {
|
|
78
|
+
const specPlans = new WeakMap();
|
|
79
|
+
const valueConverters = new WeakMap();
|
|
80
|
+
const plans = {
|
|
81
|
+
customConverters,
|
|
82
|
+
treeNumbers,
|
|
83
|
+
planOf: (spec) => cachedIn(specPlans, spec, () => compileSpecPlan(spec, plans)),
|
|
84
|
+
valueConverterOf: (spec) => cachedIn(valueConverters, spec, () => resolveConverter(spec.descriptor, plans, spec.readOptions)),
|
|
85
|
+
};
|
|
86
|
+
return plans;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* How a property's conversion error names the declaring contract, as
|
|
90
|
+
* System.Text.Json does (#485): `{"At":"03:04:60"}` on a `HolderTimeOnly`
|
|
91
|
+
* names `HolderTimeOnly`, while a value root or an element names the
|
|
92
|
+
* temporal type itself.
|
|
93
|
+
*/
|
|
94
|
+
function contractErrorNameOf(spec) {
|
|
95
|
+
return () => contractNameOf(spec);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Compiles `spec` without calling into it: nested specs and derived
|
|
99
|
+
* contracts are resolved when a value first reaches them, so the plan of a
|
|
100
|
+
* recursive spec never compiles itself.
|
|
101
|
+
*
|
|
102
|
+
* Every property's converter is resolved here, on the spec's first use, so a
|
|
103
|
+
* malformed descriptor (a hand-written spec error) fails then, even when the
|
|
104
|
+
* value lacks that property.
|
|
105
|
+
*/
|
|
106
|
+
function compileSpecPlan(spec, plans) {
|
|
107
|
+
const properties = collectWireProperties(spec);
|
|
108
|
+
const assignedIfPresent = new Set(spec.assignIfPresent);
|
|
109
|
+
const propertyPlans = properties.map((property) => ({
|
|
110
|
+
property,
|
|
111
|
+
converter: resolveConverter(property.type, plans, spec.readOptions, contractErrorNameOf(spec)),
|
|
112
|
+
assignIfPresent: assignedIfPresent.has(property.ts),
|
|
113
|
+
}));
|
|
114
|
+
const writeContract = contractWriterOf(propertyPlans);
|
|
115
|
+
const readContract = contractReaderOf(spec, propertyPlans);
|
|
116
|
+
const { polymorphism } = spec;
|
|
117
|
+
if (!polymorphism) {
|
|
118
|
+
return {
|
|
119
|
+
serialize: (value) => writeContract(value),
|
|
120
|
+
deserialize: (data) => readContract(requireJsonObject(data, spec)),
|
|
121
|
+
writeContract,
|
|
122
|
+
readContract,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
serialize: (value) => serializePolymorphic(value, spec, polymorphism, plans),
|
|
127
|
+
deserialize: (data) => deserializePolymorphic(data, spec, polymorphism, plans),
|
|
128
|
+
writeContract,
|
|
129
|
+
readContract,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Serialize a TS object to a JSON-safe plain object using the given spec. A
|
|
134
|
+
* polymorphic spec first chooses the derived contract and writes its tag
|
|
135
|
+
* before every property (FR-005); the chosen contract is never dispatched
|
|
136
|
+
* again (FR-006).
|
|
137
|
+
*/
|
|
138
|
+
export function serializeWithSpec(value, spec, customConverters) {
|
|
139
|
+
return specPlansFor(customConverters).planOf(spec).serialize(value);
|
|
140
|
+
}
|
|
141
|
+
function serializePolymorphic(value, spec, polymorphism, plans) {
|
|
142
|
+
const target = resolveSerializeTarget(value, spec, polymorphism);
|
|
143
|
+
const typeMetadata = target.tag === undefined ? undefined : { name: polymorphism.discriminator, tag: target.tag };
|
|
144
|
+
return plans.planOf(target.spec).writeContract(value, typeMetadata);
|
|
145
|
+
}
|
|
146
|
+
/** Writes a value with exactly the given properties, after the tag when one is given. */
|
|
147
|
+
function contractWriterOf(propertyPlans) {
|
|
148
|
+
const writeProperties = propertyWriterOf(propertyPlans);
|
|
149
|
+
return (value, typeMetadata) => {
|
|
150
|
+
enterWriteContainer();
|
|
151
|
+
try {
|
|
152
|
+
const result = {};
|
|
153
|
+
if (typeMetadata) {
|
|
154
|
+
result[typeMetadata.name] = typeMetadata.tag;
|
|
155
|
+
}
|
|
156
|
+
writeProperties(value, result);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
exitContainer();
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** Writes every property of `source` into `result`, in wire order. */
|
|
165
|
+
function propertyWriterOf(propertyPlans) {
|
|
166
|
+
return (source, result) => {
|
|
167
|
+
for (const { property, converter } of propertyPlans) {
|
|
168
|
+
const tsValue = source[property.ts];
|
|
169
|
+
if (tsValue == null && property.omitWhenNull) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (tsValue == null && property.optional) {
|
|
173
|
+
result[property.json] = null;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
result[property.json] = converter.serialize(tsValue);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Deserialize unknown data (from JSON.parse) into a typed instance using the
|
|
182
|
+
* spec. A polymorphic spec reads the derived contract its tag selects
|
|
183
|
+
* (FR-007). `treeNumbers` says how `data` holds its numbers; a root number
|
|
184
|
+
* leaf reads as the number it is, which no contract accepts.
|
|
185
|
+
*/
|
|
186
|
+
export function deserializeWithSpec(data, spec, customConverters, treeNumbers = "parsed") {
|
|
187
|
+
const plan = specPlansFor(customConverters, treeNumbers).planOf(spec);
|
|
188
|
+
const root = treeNumbers === "exact" ? numberOfTreeValue(data) : data;
|
|
189
|
+
// The plan reads `spec` or one of its registered derived contracts, so it builds a `T`.
|
|
190
|
+
return plan.deserialize(root);
|
|
191
|
+
}
|
|
192
|
+
function deserializePolymorphic(data, spec, polymorphism, plans) {
|
|
193
|
+
const raw = requireJsonObject(data, spec);
|
|
194
|
+
const target = resolveDeserializeTarget(raw, spec, polymorphism);
|
|
195
|
+
const metadataKey = consumedMetadataKeyOf(polymorphism);
|
|
196
|
+
return plans.planOf(target).readContract(raw, metadataKey);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The discriminator a polymorphic read consumes as metadata, never as a
|
|
200
|
+
* property; none for a hierarchy without tags, which reads like a
|
|
201
|
+
* non-polymorphic contract.
|
|
202
|
+
*/
|
|
203
|
+
function consumedMetadataKeyOf(polymorphism) {
|
|
204
|
+
return hasTaggedRegistration(polymorphism) ? polymorphism.discriminator : undefined;
|
|
205
|
+
}
|
|
206
|
+
/** `data` as a JSON object; any other JSON value cannot be converted to the contract. */
|
|
207
|
+
function requireJsonObject(data, spec) {
|
|
208
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) {
|
|
209
|
+
throw cannotConvertTo(spec);
|
|
210
|
+
}
|
|
211
|
+
return data;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Reads a JSON object with exactly the given properties of `spec` and builds
|
|
215
|
+
* it with `spec`'s factory, under `spec`'s read options.
|
|
216
|
+
*/
|
|
217
|
+
function contractReaderOf(spec, propertyPlans) {
|
|
218
|
+
const properties = propertyPlans.map(({ property }) => property);
|
|
219
|
+
const lookupPlan = propertyLookupPlanOf(properties, spec.readOptions);
|
|
220
|
+
const readProperties = propertyReaderOf(spec, propertyPlans);
|
|
221
|
+
const assignPresentMembers = presentMemberAssignerOf(propertyPlans);
|
|
222
|
+
return (raw, metadataKey) => {
|
|
223
|
+
enterReadContainer("object");
|
|
224
|
+
try {
|
|
225
|
+
const deserialized = readProperties(lookupPlan(raw, metadataKey));
|
|
226
|
+
return assignPresentMembers(spec.factory(deserialized), deserialized);
|
|
227
|
+
}
|
|
228
|
+
finally {
|
|
229
|
+
exitContainer();
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Sets every member of the contract's `assignIfPresent` list the payload
|
|
235
|
+
* carried on the constructed value, in wire order (System.Text.Json follows
|
|
236
|
+
* the payload's order; the order only shows through setter side effects).
|
|
237
|
+
*/
|
|
238
|
+
function presentMemberAssignerOf(propertyPlans) {
|
|
239
|
+
const names = propertyPlans
|
|
240
|
+
.filter(({ assignIfPresent }) => assignIfPresent)
|
|
241
|
+
.map(({ property }) => property.ts);
|
|
242
|
+
if (names.length === 0) {
|
|
243
|
+
return (value) => value;
|
|
244
|
+
}
|
|
245
|
+
return (value, deserialized) => {
|
|
246
|
+
const target = value;
|
|
247
|
+
for (const name of names) {
|
|
248
|
+
if (name in deserialized) {
|
|
249
|
+
target[name] = deserialized[name];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return value;
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Reads every property through `jsonValueOf` into the factory's input. An
|
|
257
|
+
* absent (missing or `null`) optional property reads as `null`; a missing
|
|
258
|
+
* required one throws; a missing one of the contract's `assignIfPresent`
|
|
259
|
+
* list is left out; a `null` on a non-nullable value-type property throws
|
|
260
|
+
* naming the declaring contract, as System.Text.Json does (#471).
|
|
261
|
+
*/
|
|
262
|
+
function propertyReaderOf(spec, propertyPlans) {
|
|
263
|
+
return (jsonValueOf) => {
|
|
264
|
+
const deserialized = {};
|
|
265
|
+
for (const { property, converter, assignIfPresent } of propertyPlans) {
|
|
266
|
+
const jsonValue = jsonValueOf(property);
|
|
267
|
+
if (jsonValue === undefined && assignIfPresent) {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const isAbsent = jsonValue == null;
|
|
271
|
+
if (isAbsent && property.optional) {
|
|
272
|
+
deserialized[property.ts] = null;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (jsonValue === null &&
|
|
276
|
+
!property.optional &&
|
|
277
|
+
valueTypeNameOf(property.type) !== undefined) {
|
|
278
|
+
throw cannotConvertTo(spec);
|
|
279
|
+
}
|
|
280
|
+
if (jsonValue === undefined) {
|
|
281
|
+
throw new JsonException(`Missing required property: "${property.json}"`);
|
|
282
|
+
}
|
|
283
|
+
deserialized[property.ts] = converter.deserialize(jsonValue);
|
|
284
|
+
}
|
|
285
|
+
return deserialized;
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
// ─── Value specs ─────────────────────────────────────────────────────────────
|
|
289
|
+
/**
|
|
290
|
+
* Serializes a non-object root through its descriptor, exactly as a property
|
|
291
|
+
* of that type would be (custom converters still match by kind).
|
|
292
|
+
*/
|
|
293
|
+
export function serializeWithValueSpec(value, spec, customConverters) {
|
|
294
|
+
return specPlansFor(customConverters).valueConverterOf(spec).serialize(value);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Deserializes a non-object root through its descriptor, under the spec's
|
|
298
|
+
* read options. `treeNumbers` says how `data` holds its numbers.
|
|
299
|
+
*/
|
|
300
|
+
export function deserializeWithValueSpec(data, spec, customConverters, treeNumbers = "parsed") {
|
|
301
|
+
return specPlansFor(customConverters, treeNumbers).valueConverterOf(spec).deserialize(data);
|
|
302
|
+
}
|
|
303
|
+
// ─── Memoization ─────────────────────────────────────────────────────────────
|
|
304
|
+
function cachedIn(cache, key, build) {
|
|
305
|
+
const cached = cache.get(key);
|
|
306
|
+
if (cached !== undefined) {
|
|
307
|
+
return cached;
|
|
308
|
+
}
|
|
309
|
+
const built = build();
|
|
310
|
+
cache.set(key, built);
|
|
311
|
+
return built;
|
|
312
|
+
}
|
|
@@ -38,9 +38,8 @@ export type PrimitiveDescriptor = WithClrTypeName & {
|
|
|
38
38
|
* conversion errors and decides the signedness of the integer grammars.
|
|
39
39
|
* A `[Branded]` type over a 64-bit integer adds its companion `create`
|
|
40
40
|
* factory (the valid `[JsonFactory]` member when one exists) and `typeName`,
|
|
41
|
-
* which the read applies after the exact
|
|
42
|
-
* does
|
|
43
|
-
* 012e (#450) added it.
|
|
41
|
+
* which the read applies after the exact digits, as the branded descriptor
|
|
42
|
+
* does (added by the review of slice 012e, #450).
|
|
44
43
|
*/
|
|
45
44
|
export type Int64Descriptor = {
|
|
46
45
|
readonly kind: "int64";
|
|
@@ -102,6 +101,11 @@ export type SequenceCollectionDescriptor = {
|
|
|
102
101
|
readonly kind: "list" | "queue" | "stack" | "hashSet" | "immutableList" | "immutableArray" | "readOnlyList" | "immutableHashSet" | "sortedSet";
|
|
103
102
|
readonly element: TypeDescriptor;
|
|
104
103
|
readonly create: (items: unknown[]) => Iterable<unknown>;
|
|
104
|
+
/**
|
|
105
|
+
* The CLR name of a value-type collection (`ImmutableArray<T>`), whose
|
|
106
|
+
* JSON `null` throws naming it, as System.Text.Json does (#713).
|
|
107
|
+
*/
|
|
108
|
+
readonly typeName?: string;
|
|
105
109
|
};
|
|
106
110
|
/**
|
|
107
111
|
* A runtime dictionary written as a JSON object (`Dictionary<K, V>`, the
|
|
@@ -149,10 +153,11 @@ export type EnumDescriptor = WithClrTypeName & {
|
|
|
149
153
|
export type NumericEnumDescriptor = WithClrTypeName & {
|
|
150
154
|
readonly kind: "numericEnum";
|
|
151
155
|
/**
|
|
152
|
-
* The enum's underlying integer type,
|
|
153
|
-
* key name
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
+
* The enum's underlying integer type, `System.Int32` when absent. A value
|
|
157
|
+
* read and an integer key name are range-checked by it (#369, #479); the
|
|
158
|
+
* generator names it in every slot for any type but `int`, and on a
|
|
159
|
+
* dictionary key always. A 64-bit enum (`System.Int64` / `System.UInt64`)
|
|
160
|
+
* holds `bigint`s, read and written through the int64 path (#701).
|
|
156
161
|
*/
|
|
157
162
|
readonly numberType?: IntegerTypeName;
|
|
158
163
|
readonly values: Record<string, string | number | bigint>;
|
|
@@ -232,8 +237,9 @@ export interface TypeSpec<T = unknown> {
|
|
|
232
237
|
/**
|
|
233
238
|
* The CLR name System.Text.Json prints for this struct contract
|
|
234
239
|
* (`MyApp.SPoint`, `MyApp.Outer+Inner`, ``MyApp.Pair`1[System.Int32]``);
|
|
235
|
-
* the generator emits it next to `valueType
|
|
236
|
-
*
|
|
240
|
+
* the generator emits it next to `valueType`, and on a non-polymorphic
|
|
241
|
+
* `[PlainObject]` contract, which has no constructor to name (#512).
|
|
242
|
+
* Errors name the constructor without it.
|
|
237
243
|
*/
|
|
238
244
|
readonly typeName?: string;
|
|
239
245
|
/** The context's read behavior; absent when it is System.Text.Json's default. */
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `System.Lazy<T>` with a factory: the factory runs on the first read of `value`, once. A
|
|
3
|
+
* factory that throws throws the same exception on every later read (the default
|
|
4
|
+
* `ExecutionAndPublication` mode caches it), and one that reads `value` itself throws
|
|
5
|
+
* `InvalidOperationException`.
|
|
6
|
+
*/
|
|
7
|
+
export declare class Lazy<T> {
|
|
8
|
+
private state;
|
|
9
|
+
constructor(factory: () => T);
|
|
10
|
+
/** `Lazy<T>.IsValueCreated`: `true` once the factory returned. */
|
|
11
|
+
get isValueCreated(): boolean;
|
|
12
|
+
/** `Lazy<T>.Value`. */
|
|
13
|
+
get value(): T;
|
|
14
|
+
private create;
|
|
15
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { InvalidOperationException } from "./exceptions/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* `System.Lazy<T>` with a factory: the factory runs on the first read of `value`, once. A
|
|
4
|
+
* factory that throws throws the same exception on every later read (the default
|
|
5
|
+
* `ExecutionAndPublication` mode caches it), and one that reads `value` itself throws
|
|
6
|
+
* `InvalidOperationException`.
|
|
7
|
+
*/
|
|
8
|
+
export class Lazy {
|
|
9
|
+
state;
|
|
10
|
+
constructor(factory) {
|
|
11
|
+
this.state = { kind: "pending", factory };
|
|
12
|
+
}
|
|
13
|
+
/** `Lazy<T>.IsValueCreated`: `true` once the factory returned. */
|
|
14
|
+
get isValueCreated() {
|
|
15
|
+
return this.state.kind === "created";
|
|
16
|
+
}
|
|
17
|
+
/** `Lazy<T>.Value`. */
|
|
18
|
+
get value() {
|
|
19
|
+
const state = this.state;
|
|
20
|
+
if (state.kind === "created") {
|
|
21
|
+
return state.value;
|
|
22
|
+
}
|
|
23
|
+
if (state.kind === "failed") {
|
|
24
|
+
throw state.error;
|
|
25
|
+
}
|
|
26
|
+
if (state.kind === "running") {
|
|
27
|
+
throw new InvalidOperationException("ValueFactory attempted to access the Value property of this instance.");
|
|
28
|
+
}
|
|
29
|
+
return this.create(state.factory);
|
|
30
|
+
}
|
|
31
|
+
create(factory) {
|
|
32
|
+
this.state = { kind: "running" };
|
|
33
|
+
try {
|
|
34
|
+
const value = factory();
|
|
35
|
+
this.state = { kind: "created", value };
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
this.state = { kind: "failed", error };
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -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, SelectManyOp, SelectOp, SkipOp, SkipWhileOp, TakeOp, TakeWhileOp, UnionOp, WhereOp, ZipOp, AggregateTerm, AllTerm, AnyTerm, AverageTerm, AverageInt64Term, ContainsTerm, CountTerm, 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, } 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, } 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, } 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, } 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";
|
|
@@ -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
|
+
}
|
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composition operator factories — each returns a tagged descriptor.
|
|
3
|
+
*
|
|
4
|
+
* `apply` walks the source via a generator wrapped behind
|
|
5
|
+
* `[Symbol.iterator]` so each consumer creates a fresh iterator
|
|
6
|
+
* (re-enumerable like .NET LINQ). The descriptor carries the raw
|
|
7
|
+
* lambdas verbatim so an IQueryable provider can introspect the chain
|
|
8
|
+
* without parsing closures.
|
|
9
|
+
*
|
|
10
|
+
* Lazy semantics: composition allocates only the descriptor; the
|
|
11
|
+
* generator runs on `[Symbol.iterator]()` from the consumer side.
|
|
12
|
+
* Short-circuit terminals (`first`, `take(n)`) stop iterating as soon
|
|
13
|
+
* as their result is determined.
|
|
14
|
+
*/
|
|
15
|
+
import type { IEqualityComparer } from "../collections/default-comparer.ts";
|
|
1
16
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
2
|
-
import type { AppendOp, ConcatOp, DistinctByOp, DistinctOp, ExceptOp, IntersectOp, OrderByDescendingOp, OrderByOp, PrependOp, ReverseOp, 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";
|
|
3
18
|
export declare function where<T>(predicate: (item: T, index: number) => boolean, queryable?: QueryableMeta): WhereOp<T>;
|
|
4
19
|
export declare function select<T, R>(selector: (item: T, index: number) => R, queryable?: QueryableMeta): SelectOp<T, R>;
|
|
5
20
|
export declare function selectMany<T, R>(selector: (item: T, index: number) => Iterable<R>, queryable?: QueryableMeta): SelectManyOp<T, R>;
|
|
@@ -7,7 +22,14 @@ export declare function take<T>(count: number): TakeOp<T>;
|
|
|
7
22
|
export declare function skip<T>(count: number): SkipOp<T>;
|
|
8
23
|
export declare function takeWhile<T>(predicate: (item: T, index: number) => boolean, queryable?: QueryableMeta): TakeWhileOp<T>;
|
|
9
24
|
export declare function skipWhile<T>(predicate: (item: T, index: number) => boolean, queryable?: QueryableMeta): SkipWhileOp<T>;
|
|
10
|
-
|
|
25
|
+
/** `Distinct()` / `Distinct(comparer)`: the first of each element, equal by the comparer or by value. */
|
|
26
|
+
export declare function distinct<T>(comparer?: IEqualityComparer<T> | null): DistinctOp<T>;
|
|
27
|
+
/**
|
|
28
|
+
* `DefaultIfEmpty()` / `DefaultIfEmpty(defaultValue)`: without a `defaultValue` an empty
|
|
29
|
+
* source yields `null`; with one, the value. `T` comes from the surrounding `linq(...)` call.
|
|
30
|
+
*/
|
|
31
|
+
export declare function defaultIfEmpty<T>(defaultValue?: null): DefaultIfEmptyOp<T, null>;
|
|
32
|
+
export declare function defaultIfEmpty<T>(defaultValue: NoInfer<T>): DefaultIfEmptyOp<T, T>;
|
|
11
33
|
export declare function distinctBy<T, K>(keySelector: (item: T) => K, queryable?: QueryableMeta): DistinctByOp<T, K>;
|
|
12
34
|
export declare function concat<T>(other: Iterable<T>): ConcatOp<T>;
|
|
13
35
|
/** `Union(second)`: the distinct elements of the source, then those of `other` it lacks. */
|
|
@@ -33,8 +55,16 @@ export declare function thenByDescending<T, K>(keySelector: (item: T) => K, quer
|
|
|
33
55
|
/**
|
|
34
56
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
35
57
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
36
|
-
* items in source order
|
|
37
|
-
*
|
|
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.
|
|
61
|
+
*/
|
|
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.
|
|
38
68
|
*/
|
|
39
|
-
export declare function
|
|
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>;
|
|
40
70
|
export declare function zip<T, U, R>(other: Iterable<U>, resultSelector: (first: T, second: U) => R, queryable?: QueryableMeta): ZipOp<T, U, R>;
|