metano-runtime 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/dist/system/char-members.d.ts +23 -0
  2. package/dist/system/char-members.js +46 -0
  3. package/dist/system/collections/hash-core.d.ts +5 -0
  4. package/dist/system/collections/hash-core.js +24 -0
  5. package/dist/system/collections/helpers.d.ts +16 -3
  6. package/dist/system/collections/helpers.js +28 -2
  7. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  8. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  9. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  10. package/dist/system/collections/immutable-list-core.js +19 -1
  11. package/dist/system/collections/immutable-storage.d.ts +2 -0
  12. package/dist/system/collections/immutable-storage.js +6 -1
  13. package/dist/system/collections/interfaces.d.ts +13 -0
  14. package/dist/system/collections/list.d.ts +3 -0
  15. package/dist/system/collections/list.js +7 -0
  16. package/dist/system/collections/read-only-list.d.ts +11 -0
  17. package/dist/system/collections/read-only-list.js +26 -1
  18. package/dist/system/collections/sorted-list.d.ts +5 -0
  19. package/dist/system/collections/sorted-list.js +12 -1
  20. package/dist/system/decimal-parsing.d.ts +12 -0
  21. package/dist/system/decimal-parsing.js +86 -0
  22. package/dist/system/exceptions/index.d.ts +1 -0
  23. package/dist/system/exceptions/index.js +1 -0
  24. package/dist/system/exceptions/throw-error.d.ts +7 -0
  25. package/dist/system/exceptions/throw-error.js +9 -0
  26. package/dist/system/int64.d.ts +6 -0
  27. package/dist/system/int64.js +18 -9
  28. package/dist/system/integer-division.d.ts +12 -4
  29. package/dist/system/integer-division.js +19 -2
  30. package/dist/system/json/json-serializer.d.ts +12 -1
  31. package/dist/system/json/json-serializer.js +28 -0
  32. package/dist/system/json/json-text-writer.d.ts +17 -0
  33. package/dist/system/json/json-text-writer.js +175 -0
  34. package/dist/system/json/number-from-string.js +20 -6
  35. package/dist/system/json/numeric-converters.js +41 -0
  36. package/dist/system/linq/grouping.d.ts +12 -0
  37. package/dist/system/linq/grouping.js +42 -0
  38. package/dist/system/linq/index.d.ts +4 -3
  39. package/dist/system/linq/index.js +3 -2
  40. package/dist/system/linq/linq.js +20 -1
  41. package/dist/system/linq/lookup.d.ts +17 -0
  42. package/dist/system/linq/lookup.js +20 -0
  43. package/dist/system/linq/operators.d.ts +12 -4
  44. package/dist/system/linq/operators.js +61 -15
  45. package/dist/system/linq/terminals.d.ts +29 -2
  46. package/dist/system/linq/terminals.js +71 -3
  47. package/dist/system/linq/types.d.ts +27 -4
  48. package/dist/system/math.d.ts +7 -0
  49. package/dist/system/math.js +14 -0
  50. package/dist/system/number-parsing.d.ts +16 -4
  51. package/dist/system/number-parsing.js +21 -4
  52. package/dist-unchecked/system/char-members.d.ts +23 -0
  53. package/dist-unchecked/system/char-members.js +46 -0
  54. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  55. package/dist-unchecked/system/collections/hash-core.js +24 -0
  56. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  57. package/dist-unchecked/system/collections/helpers.js +28 -2
  58. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  59. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  60. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  61. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  62. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  63. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  64. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  65. package/dist-unchecked/system/collections/list.d.ts +3 -0
  66. package/dist-unchecked/system/collections/list.js +7 -0
  67. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  68. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  69. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  70. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  71. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  72. package/dist-unchecked/system/decimal-parsing.js +86 -0
  73. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  74. package/dist-unchecked/system/exceptions/index.js +1 -0
  75. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  76. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  77. package/dist-unchecked/system/int64.d.ts +6 -0
  78. package/dist-unchecked/system/int64.js +18 -9
  79. package/dist-unchecked/system/integer-division.d.ts +12 -4
  80. package/dist-unchecked/system/integer-division.js +19 -2
  81. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  82. package/dist-unchecked/system/json/json-serializer.js +28 -0
  83. package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
  84. package/dist-unchecked/system/json/json-text-writer.js +175 -0
  85. package/dist-unchecked/system/json/number-from-string.js +20 -6
  86. package/dist-unchecked/system/json/numeric-converters.js +41 -0
  87. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  88. package/dist-unchecked/system/linq/grouping.js +42 -0
  89. package/dist-unchecked/system/linq/index.d.ts +4 -3
  90. package/dist-unchecked/system/linq/index.js +3 -2
  91. package/dist-unchecked/system/linq/linq.js +20 -1
  92. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  93. package/dist-unchecked/system/linq/lookup.js +20 -0
  94. package/dist-unchecked/system/linq/operators.d.ts +12 -4
  95. package/dist-unchecked/system/linq/operators.js +61 -15
  96. package/dist-unchecked/system/linq/terminals.d.ts +29 -2
  97. package/dist-unchecked/system/linq/terminals.js +71 -3
  98. package/dist-unchecked/system/linq/types.d.ts +27 -4
  99. package/dist-unchecked/system/math.d.ts +7 -0
  100. package/dist-unchecked/system/math.js +14 -0
  101. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  102. package/dist-unchecked/system/number-parsing.js +21 -4
  103. package/package.json +1 -1
  104. package/src/system/char-members.ts +57 -0
  105. package/src/system/collections/hash-core.ts +31 -0
  106. package/src/system/collections/helpers.ts +39 -4
  107. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  108. package/src/system/collections/immutable-list-core.ts +23 -1
  109. package/src/system/collections/immutable-storage.ts +8 -1
  110. package/src/system/collections/interfaces.ts +16 -0
  111. package/src/system/collections/list.ts +9 -0
  112. package/src/system/collections/read-only-list.ts +35 -1
  113. package/src/system/collections/sorted-list.ts +15 -1
  114. package/src/system/decimal-parsing.ts +109 -0
  115. package/src/system/exceptions/index.ts +1 -0
  116. package/src/system/exceptions/throw-error.ts +9 -0
  117. package/src/system/int64.ts +27 -10
  118. package/src/system/integer-division.ts +43 -6
  119. package/src/system/json/json-serializer.ts +41 -0
  120. package/src/system/json/json-text-writer.ts +224 -0
  121. package/src/system/json/number-from-string.ts +26 -6
  122. package/src/system/json/numeric-converters.ts +55 -0
  123. package/src/system/linq/grouping.ts +77 -0
  124. package/src/system/linq/index.ts +8 -0
  125. package/src/system/linq/linq.ts +23 -1
  126. package/src/system/linq/lookup.ts +34 -0
  127. package/src/system/linq/operators.ts +78 -15
  128. package/src/system/linq/terminals.ts +85 -3
  129. package/src/system/linq/types.ts +32 -0
  130. package/src/system/math.ts +19 -0
  131. package/src/system/number-parsing.ts +24 -4
@@ -9,15 +9,18 @@
9
9
  * Short-circuit terminals (`first`, `any`, `contains`) stop iterating
10
10
  * as soon as the answer is known.
11
11
  */
12
+ import { elementEquals, type IEqualityComparer } from "../collections/default-comparer.ts";
12
13
  import { Dictionary } from "../collections/dictionary.ts";
13
14
  import { HashSet } from "../collections/hash-set.ts";
14
- import { throwOOR } from "../collections/throw-helpers.ts";
15
+ import { throwArgumentNull, throwOOR } from "../collections/throw-helpers.ts";
15
16
  import { List } from "../collections/list.ts";
16
17
  import { ArgumentOutOfRangeException } from "../exceptions/argument-out-of-range-exception.ts";
17
18
  import { InvalidOperationException } from "../exceptions/invalid-operation-exception.ts";
18
19
  import type { RuntimeType } from "../reflection/runtime-type.ts";
19
20
  import { compareKeys } from "./compare-keys.ts";
20
21
  import type { QueryableMeta } from "./expr-tree.ts";
22
+ import { bucketsOf } from "./grouping.ts";
23
+ import { Lookup } from "./lookup.ts";
21
24
  import type {
22
25
  AggregateTerm,
23
26
  AllTerm,
@@ -37,11 +40,13 @@ import type {
37
40
  MinTerm,
38
41
  SingleOrDefaultTerm,
39
42
  SingleTerm,
43
+ SequenceEqualTerm,
40
44
  SumTerm,
41
45
  ToArrayTerm,
42
46
  ToDictionaryTerm,
43
47
  ToHashSetTerm,
44
48
  ToListTerm,
49
+ ToLookupTerm,
45
50
  ToMapTerm,
46
51
  ToSetTerm,
47
52
  } from "./types.ts";
@@ -602,17 +607,94 @@ export function averageNullable<T>(
602
607
  };
603
608
  }
604
609
 
605
- export function contains<T>(value: T): ContainsTerm<T> {
610
+ /**
611
+ * `Contains(value[, comparer])`: whether an element equals `value` by `comparer`, or, without
612
+ * one, by .NET's `EqualityComparer<T>.Default` (a record, `NaN` and a decimal by value).
613
+ */
614
+ export function contains<T>(
615
+ value: T,
616
+ comparer?: IEqualityComparer<NoInfer<T>> | null,
617
+ ): ContainsTerm<T> {
606
618
  return {
607
619
  kind: "contains",
608
620
  value,
621
+ comparer,
609
622
  apply: (source) => {
610
- for (const element of source) if (element === value) return true;
623
+ // A primitive other than NaN is equal to exactly what `===` says, with no comparer. `null`
624
+ // is left out: generated code can hold `undefined` where C# has null.
625
+ if (!comparer && value != null && typeof value !== "object" && !Number.isNaN(value)) {
626
+ for (const element of source) if (element === value) return true;
627
+ return false;
628
+ }
629
+
630
+ for (const element of source) if (equalsBy(comparer, element, value)) return true;
611
631
  return false;
612
632
  },
613
633
  };
614
634
  }
615
635
 
636
+ /**
637
+ * `SequenceEqual(second[, comparer])`: whether both sequences have the same length and equal
638
+ * elements in order, equal by `comparer` or by `EqualityComparer<T>.Default`.
639
+ */
640
+ export function sequenceEqual<T>(
641
+ second: Iterable<T>,
642
+ comparer?: IEqualityComparer<NoInfer<T>> | null,
643
+ ): SequenceEqualTerm<T> {
644
+ return {
645
+ kind: "sequenceEqual",
646
+ second,
647
+ comparer,
648
+ apply: (source) => {
649
+ if (second === null || second === undefined) throwArgumentNull("second");
650
+
651
+ const first = source[Symbol.iterator]();
652
+ const other = second[Symbol.iterator]();
653
+
654
+ try {
655
+ while (true) {
656
+ const a = first.next();
657
+ const b = other.next();
658
+
659
+ if (a.done || b.done) return a.done === b.done;
660
+ if (!equalsBy(comparer, a.value, b.value)) return false;
661
+ }
662
+ } finally {
663
+ first.return?.();
664
+ other.return?.();
665
+ }
666
+ },
667
+ };
668
+ }
669
+
670
+ /**
671
+ * `ToLookup(keySelector[, elementSelector][, comparer])`: a `Lookup` of the elements by key.
672
+ * The second argument is the `elementSelector` function or, in the `(keySelector, comparer)`
673
+ * overload, the comparer object.
674
+ */
675
+ export function toLookup<T, K, V = T>(
676
+ keySelector: (item: T) => K,
677
+ elementSelector?: ((item: T) => V) | IEqualityComparer<NoInfer<K>> | null,
678
+ comparer?: IEqualityComparer<NoInfer<K>> | null,
679
+ ): ToLookupTerm<T, K, V> {
680
+ const selectsElement = typeof elementSelector === "function";
681
+ const keyComparer = selectsElement ? comparer : (elementSelector ?? comparer);
682
+ const select = selectsElement ? elementSelector : undefined;
683
+
684
+ return {
685
+ kind: "toLookup",
686
+ keySelector,
687
+ elementSelector: select,
688
+ comparer: keyComparer,
689
+ apply: (source) => new Lookup(bucketsOf(source, keySelector, keyComparer, select)),
690
+ };
691
+ }
692
+
693
+ /** `comparer.Equals(a, b)`, or `EqualityComparer<T>.Default` without a comparer. */
694
+ function equalsBy<T>(comparer: IEqualityComparer<T> | null | undefined, a: T, b: T): boolean {
695
+ return comparer ? comparer.equals(a, b) : elementEquals(a, b);
696
+ }
697
+
616
698
  export function aggregate<T, A>(seed: A, accumulator: (acc: A, item: T) => A): AggregateTerm<T, A> {
617
699
  return {
618
700
  kind: "aggregate",
@@ -3,6 +3,7 @@ import type { Dictionary } from "../collections/dictionary.ts";
3
3
  import type { HashSet } from "../collections/hash-set.ts";
4
4
  import type { List } from "../collections/list.ts";
5
5
  import type { QueryableMeta } from "./expr-tree.ts";
6
+ import type { Lookup } from "./lookup.ts";
6
7
 
7
8
  /**
8
9
  * Descriptor-based LINQ runtime — prototype for #20.
@@ -47,6 +48,7 @@ export type OperatorKind =
47
48
  | "thenBy"
48
49
  | "thenByDescending"
49
50
  | "groupBy"
51
+ | "join"
50
52
  | "zip"
51
53
  | "defaultIfEmpty";
52
54
 
@@ -57,6 +59,8 @@ export type TerminalKind =
57
59
  | "toList"
58
60
  | "toDictionary"
59
61
  | "toHashSet"
62
+ | "toLookup"
63
+ | "sequenceEqual"
60
64
  | "first"
61
65
  | "firstOrDefault"
62
66
  | "last"
@@ -235,9 +239,20 @@ export interface Grouping<K, T> extends Iterable<T> {
235
239
  export interface GroupByOp<T, K> extends OperatorBase<T, Grouping<K, T>> {
236
240
  readonly kind: "groupBy";
237
241
  readonly keySelector: (item: T) => K;
242
+ readonly comparer?: IEqualityComparer<K> | null;
238
243
  readonly queryable?: QueryableMeta;
239
244
  }
240
245
 
246
+ /** `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`. */
247
+ export interface JoinOp<T, U, K, R> extends OperatorBase<T, R> {
248
+ readonly kind: "join";
249
+ readonly inner: Iterable<U>;
250
+ readonly outerKeySelector: (item: T) => K;
251
+ readonly innerKeySelector: (item: U) => K;
252
+ readonly resultSelector: (outer: T, inner: U) => R;
253
+ readonly comparer?: IEqualityComparer<K> | null;
254
+ }
255
+
241
256
  /** Discriminated union of every composition operator. */
242
257
  export type AnyOperator =
243
258
  | WhereOp<unknown>
@@ -261,6 +276,7 @@ export type AnyOperator =
261
276
  | ThenByOp<unknown, unknown>
262
277
  | ThenByDescendingOp<unknown, unknown>
263
278
  | GroupByOp<unknown, unknown>
279
+ | JoinOp<unknown, unknown, unknown, unknown>
264
280
  | ZipOp<unknown, unknown, unknown>
265
281
  | DefaultIfEmptyOp<unknown>;
266
282
 
@@ -474,6 +490,20 @@ export interface MaxInt64Term<T, R extends bigint | null> extends TerminalBase<T
474
490
  export interface ContainsTerm<T> extends TerminalBase<T, boolean> {
475
491
  readonly kind: "contains";
476
492
  readonly value: T;
493
+ readonly comparer?: IEqualityComparer<T> | null;
494
+ }
495
+
496
+ export interface SequenceEqualTerm<T> extends TerminalBase<T, boolean> {
497
+ readonly kind: "sequenceEqual";
498
+ readonly second: Iterable<T>;
499
+ readonly comparer?: IEqualityComparer<T> | null;
500
+ }
501
+
502
+ export interface ToLookupTerm<T, K, V> extends TerminalBase<T, Lookup<K, V>> {
503
+ readonly kind: "toLookup";
504
+ readonly keySelector: (item: T) => K;
505
+ readonly elementSelector?: (item: T) => V;
506
+ readonly comparer?: IEqualityComparer<K> | null;
477
507
  }
478
508
 
479
509
  export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
@@ -512,4 +542,6 @@ export type AnyTerminal =
512
542
  | MinInt64Term<unknown, bigint | null>
513
543
  | MaxInt64Term<unknown, bigint | null>
514
544
  | ContainsTerm<unknown>
545
+ | SequenceEqualTerm<unknown>
546
+ | ToLookupTerm<unknown, unknown, unknown>
515
547
  | AggregateTerm<unknown, unknown>;
@@ -11,6 +11,7 @@
11
11
 
12
12
  import { ArgumentException } from "./exceptions/argument-exception.ts";
13
13
  import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.ts";
14
+ import { OverflowException } from "./exceptions/overflow-exception.ts";
14
15
 
15
16
  /**
16
17
  * Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
@@ -180,3 +181,21 @@ function assertMode(mode: number): asserts mode is MidpointRounding {
180
181
  );
181
182
  }
182
183
  }
184
+
185
+ const INT32_MIN = -2147483648;
186
+
187
+ /**
188
+ * `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
189
+ * `int.MinValue`, whose magnitude does not fit `int`.
190
+ *
191
+ * @throws OverflowException, with .NET's message, for `int.MinValue`.
192
+ */
193
+ export function absInt32(value: number): number {
194
+ if (value === INT32_MIN) {
195
+ throw new OverflowException(
196
+ "Negating the minimum value of a twos complement number is invalid.",
197
+ );
198
+ }
199
+
200
+ return Math.abs(value);
201
+ }
@@ -156,8 +156,7 @@ function numberResult(result: readonly [found: boolean, value: bigint]): ParseRe
156
156
  }
157
157
 
158
158
  /**
159
- * Mirrors `double.TryParse(string, out double)` (and `float.TryParse`, whose values the
160
- * generated code holds as `number` too). A value beyond the `double` range parses to
159
+ * Mirrors `double.TryParse(string, out double)`. A value beyond the `double` range parses to
161
160
  * an infinity, as it does since .NET Core 3.0.
162
161
  */
163
162
  export function tryParseFloat64(text: string | null | undefined): ParseResult<number> {
@@ -176,6 +175,17 @@ export function tryParseFloat64(text: string | null | undefined): ParseResult<nu
176
175
  return FLOAT.test(trimmed) ? [true, Number(trimmed.replace(/,/g, ""))] : [false, 0];
177
176
  }
178
177
 
178
+ /**
179
+ * Mirrors `float.TryParse(string, out float)`: {@link tryParseFloat64}'s grammar, the value
180
+ * rounded to single precision as .NET's parser rounds it (#695). A value beyond the
181
+ * `float` range parses to an infinity.
182
+ */
183
+ export function tryParseFloat32(text: string | null | undefined): ParseResult<number> {
184
+ const [found, value] = tryParseFloat64(text);
185
+
186
+ return [found, Math.fround(value)];
187
+ }
188
+
179
189
  /**
180
190
  * Mirrors `bool.TryParse(string, out bool)`: `true` / `false` in any letter case, around
181
191
  * whitespace (JavaScript's `\s`, close to .NET's `char.IsWhiteSpace`) and `\0` characters.
@@ -191,8 +201,7 @@ export function tryParseBoolean(text: string | null | undefined): ParseResult<bo
191
201
  }
192
202
 
193
203
  /**
194
- * Mirrors `double.Parse(string)` (and `float.Parse`, whose values the generated code holds as
195
- * `number` too).
204
+ * Mirrors `double.Parse(string)`.
196
205
  *
197
206
  * @throws ArgumentNullException for `null`.
198
207
  * @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
@@ -211,6 +220,17 @@ export function parseFloat64(text: string | null | undefined): number {
211
220
  return value;
212
221
  }
213
222
 
223
+ /**
224
+ * Mirrors `float.Parse(string)`: {@link parseFloat64}, the value rounded to single precision
225
+ * as .NET's parser rounds it (#695).
226
+ *
227
+ * @throws ArgumentNullException for `null`.
228
+ * @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
229
+ */
230
+ export function parseFloat32(text: string | null | undefined): number {
231
+ return Math.fround(parseFloat64(text));
232
+ }
233
+
214
234
  /**
215
235
  * Mirrors `bool.Parse(string)`.
216
236
  *