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.
Files changed (211) hide show
  1. package/dist/system/char-members.d.ts +51 -0
  2. package/dist/system/char-members.js +108 -0
  3. package/dist/system/collections/array-helpers.d.ts +28 -0
  4. package/dist/system/collections/array-helpers.js +105 -0
  5. package/dist/system/collections/hash-core.d.ts +5 -0
  6. package/dist/system/collections/hash-core.js +24 -0
  7. package/dist/system/collections/helpers.d.ts +16 -3
  8. package/dist/system/collections/helpers.js +28 -2
  9. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  10. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  11. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  12. package/dist/system/collections/immutable-list-core.js +19 -1
  13. package/dist/system/collections/immutable-storage.d.ts +2 -0
  14. package/dist/system/collections/immutable-storage.js +6 -1
  15. package/dist/system/collections/index.d.ts +2 -0
  16. package/dist/system/collections/index.js +2 -0
  17. package/dist/system/collections/interfaces.d.ts +13 -0
  18. package/dist/system/collections/list.d.ts +3 -0
  19. package/dist/system/collections/list.js +8 -11
  20. package/dist/system/collections/read-only-list.d.ts +11 -0
  21. package/dist/system/collections/read-only-list.js +26 -1
  22. package/dist/system/collections/sorted-list.d.ts +5 -0
  23. package/dist/system/collections/sorted-list.js +12 -1
  24. package/dist/system/collections/string-comparer.d.ts +14 -0
  25. package/dist/system/collections/string-comparer.js +45 -0
  26. package/dist/system/decimal-parsing.d.ts +12 -0
  27. package/dist/system/decimal-parsing.js +86 -0
  28. package/dist/system/exceptions/index.d.ts +1 -0
  29. package/dist/system/exceptions/index.js +1 -0
  30. package/dist/system/exceptions/throw-error.d.ts +7 -0
  31. package/dist/system/exceptions/throw-error.js +9 -0
  32. package/dist/system/index.d.ts +5 -0
  33. package/dist/system/index.js +5 -0
  34. package/dist/system/int64.d.ts +6 -0
  35. package/dist/system/int64.js +18 -9
  36. package/dist/system/integer-division.d.ts +12 -4
  37. package/dist/system/integer-division.js +19 -2
  38. package/dist/system/json/contract-name.d.ts +2 -1
  39. package/dist/system/json/contract-name.js +3 -2
  40. package/dist/system/json/converters.d.ts +15 -52
  41. package/dist/system/json/converters.js +33 -317
  42. package/dist/system/json/dictionary-keys.js +1 -3
  43. package/dist/system/json/index.d.ts +1 -1
  44. package/dist/system/json/index.js +1 -1
  45. package/dist/system/json/json-serializer.d.ts +12 -1
  46. package/dist/system/json/json-serializer.js +29 -1
  47. package/dist/system/json/json-text-writer.d.ts +17 -0
  48. package/dist/system/json/json-text-writer.js +175 -0
  49. package/dist/system/json/null-reads.d.ts +2 -1
  50. package/dist/system/json/null-reads.js +3 -1
  51. package/dist/system/json/number-from-string.d.ts +2 -0
  52. package/dist/system/json/number-from-string.js +24 -6
  53. package/dist/system/json/numeric-converters.d.ts +4 -4
  54. package/dist/system/json/numeric-converters.js +46 -8
  55. package/dist/system/json/numeric-enum-members.d.ts +3 -3
  56. package/dist/system/json/numeric-enum-members.js +2 -4
  57. package/dist/system/json/spec-plans.d.ts +102 -0
  58. package/dist/system/json/spec-plans.js +312 -0
  59. package/dist/system/json/types.d.ts +15 -9
  60. package/dist/system/lazy.d.ts +15 -0
  61. package/dist/system/lazy.js +43 -0
  62. package/dist/system/linq/grouping.d.ts +12 -0
  63. package/dist/system/linq/grouping.js +42 -0
  64. package/dist/system/linq/index.d.ts +4 -3
  65. package/dist/system/linq/index.js +3 -2
  66. package/dist/system/linq/linq.js +20 -1
  67. package/dist/system/linq/lookup.d.ts +17 -0
  68. package/dist/system/linq/lookup.js +20 -0
  69. package/dist/system/linq/operators.d.ts +35 -5
  70. package/dist/system/linq/operators.js +82 -31
  71. package/dist/system/linq/terminals.d.ts +41 -2
  72. package/dist/system/linq/terminals.js +122 -2
  73. package/dist/system/linq/types.d.ts +48 -4
  74. package/dist/system/math.d.ts +7 -0
  75. package/dist/system/math.js +14 -0
  76. package/dist/system/number-members.d.ts +10 -0
  77. package/dist/system/number-members.js +25 -0
  78. package/dist/system/number-parsing.d.ts +16 -4
  79. package/dist/system/number-parsing.js +21 -4
  80. package/dist/system/string-builder.d.ts +30 -0
  81. package/dist/system/string-builder.js +86 -0
  82. package/dist/system/task-completion-source.d.ts +37 -0
  83. package/dist/system/task-completion-source.js +81 -0
  84. package/dist-unchecked/system/char-members.d.ts +51 -0
  85. package/dist-unchecked/system/char-members.js +108 -0
  86. package/dist-unchecked/system/collections/array-helpers.d.ts +28 -0
  87. package/dist-unchecked/system/collections/array-helpers.js +105 -0
  88. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  89. package/dist-unchecked/system/collections/hash-core.js +24 -0
  90. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  91. package/dist-unchecked/system/collections/helpers.js +28 -2
  92. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  93. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  94. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  95. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  96. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  97. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  98. package/dist-unchecked/system/collections/index.d.ts +2 -0
  99. package/dist-unchecked/system/collections/index.js +2 -0
  100. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  101. package/dist-unchecked/system/collections/list.d.ts +3 -0
  102. package/dist-unchecked/system/collections/list.js +8 -11
  103. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  104. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  105. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  106. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  107. package/dist-unchecked/system/collections/string-comparer.d.ts +14 -0
  108. package/dist-unchecked/system/collections/string-comparer.js +45 -0
  109. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  110. package/dist-unchecked/system/decimal-parsing.js +86 -0
  111. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  112. package/dist-unchecked/system/exceptions/index.js +1 -0
  113. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  114. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  115. package/dist-unchecked/system/index.d.ts +5 -0
  116. package/dist-unchecked/system/index.js +5 -0
  117. package/dist-unchecked/system/int64.d.ts +6 -0
  118. package/dist-unchecked/system/int64.js +18 -9
  119. package/dist-unchecked/system/integer-division.d.ts +12 -4
  120. package/dist-unchecked/system/integer-division.js +19 -2
  121. package/dist-unchecked/system/json/contract-name.d.ts +2 -1
  122. package/dist-unchecked/system/json/contract-name.js +3 -2
  123. package/dist-unchecked/system/json/converters.d.ts +15 -52
  124. package/dist-unchecked/system/json/converters.js +33 -317
  125. package/dist-unchecked/system/json/dictionary-keys.js +1 -3
  126. package/dist-unchecked/system/json/index.d.ts +1 -1
  127. package/dist-unchecked/system/json/index.js +1 -1
  128. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  129. package/dist-unchecked/system/json/json-serializer.js +29 -1
  130. package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
  131. package/dist-unchecked/system/json/json-text-writer.js +175 -0
  132. package/dist-unchecked/system/json/null-reads.d.ts +2 -1
  133. package/dist-unchecked/system/json/null-reads.js +3 -1
  134. package/dist-unchecked/system/json/number-from-string.d.ts +2 -0
  135. package/dist-unchecked/system/json/number-from-string.js +24 -6
  136. package/dist-unchecked/system/json/numeric-converters.d.ts +4 -4
  137. package/dist-unchecked/system/json/numeric-converters.js +46 -8
  138. package/dist-unchecked/system/json/numeric-enum-members.d.ts +3 -3
  139. package/dist-unchecked/system/json/numeric-enum-members.js +2 -4
  140. package/dist-unchecked/system/json/spec-plans.d.ts +102 -0
  141. package/dist-unchecked/system/json/spec-plans.js +312 -0
  142. package/dist-unchecked/system/json/types.d.ts +15 -9
  143. package/dist-unchecked/system/lazy.d.ts +15 -0
  144. package/dist-unchecked/system/lazy.js +43 -0
  145. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  146. package/dist-unchecked/system/linq/grouping.js +42 -0
  147. package/dist-unchecked/system/linq/index.d.ts +4 -3
  148. package/dist-unchecked/system/linq/index.js +3 -2
  149. package/dist-unchecked/system/linq/linq.js +20 -1
  150. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  151. package/dist-unchecked/system/linq/lookup.js +20 -0
  152. package/dist-unchecked/system/linq/operators.d.ts +35 -5
  153. package/dist-unchecked/system/linq/operators.js +82 -31
  154. package/dist-unchecked/system/linq/terminals.d.ts +41 -2
  155. package/dist-unchecked/system/linq/terminals.js +122 -2
  156. package/dist-unchecked/system/linq/types.d.ts +48 -4
  157. package/dist-unchecked/system/math.d.ts +7 -0
  158. package/dist-unchecked/system/math.js +14 -0
  159. package/dist-unchecked/system/number-members.d.ts +10 -0
  160. package/dist-unchecked/system/number-members.js +25 -0
  161. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  162. package/dist-unchecked/system/number-parsing.js +21 -4
  163. package/dist-unchecked/system/string-builder.d.ts +30 -0
  164. package/dist-unchecked/system/string-builder.js +86 -0
  165. package/dist-unchecked/system/task-completion-source.d.ts +37 -0
  166. package/dist-unchecked/system/task-completion-source.js +81 -0
  167. package/package.json +1 -1
  168. package/src/system/char-members.ts +131 -0
  169. package/src/system/collections/array-helpers.ts +139 -0
  170. package/src/system/collections/hash-core.ts +31 -0
  171. package/src/system/collections/helpers.ts +39 -4
  172. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  173. package/src/system/collections/immutable-list-core.ts +23 -1
  174. package/src/system/collections/immutable-storage.ts +8 -1
  175. package/src/system/collections/index.ts +2 -0
  176. package/src/system/collections/interfaces.ts +16 -0
  177. package/src/system/collections/list.ts +10 -15
  178. package/src/system/collections/read-only-list.ts +35 -1
  179. package/src/system/collections/sorted-list.ts +15 -1
  180. package/src/system/collections/string-comparer.ts +57 -0
  181. package/src/system/decimal-parsing.ts +109 -0
  182. package/src/system/exceptions/index.ts +1 -0
  183. package/src/system/exceptions/throw-error.ts +9 -0
  184. package/src/system/index.ts +5 -0
  185. package/src/system/int64.ts +27 -10
  186. package/src/system/integer-division.ts +43 -6
  187. package/src/system/json/contract-name.ts +3 -2
  188. package/src/system/json/converters.ts +38 -526
  189. package/src/system/json/dictionary-keys.ts +6 -4
  190. package/src/system/json/index.ts +1 -1
  191. package/src/system/json/json-serializer.ts +42 -1
  192. package/src/system/json/json-text-writer.ts +224 -0
  193. package/src/system/json/null-reads.ts +3 -1
  194. package/src/system/json/number-from-string.ts +31 -6
  195. package/src/system/json/numeric-converters.ts +60 -8
  196. package/src/system/json/numeric-enum-members.ts +4 -6
  197. package/src/system/json/spec-plans.ts +524 -0
  198. package/src/system/json/types.ts +15 -9
  199. package/src/system/lazy.ts +62 -0
  200. package/src/system/linq/grouping.ts +77 -0
  201. package/src/system/linq/index.ts +14 -0
  202. package/src/system/linq/linq.ts +23 -1
  203. package/src/system/linq/lookup.ts +34 -0
  204. package/src/system/linq/operators.ts +109 -17
  205. package/src/system/linq/terminals.ts +161 -2
  206. package/src/system/linq/types.ts +66 -3
  207. package/src/system/math.ts +19 -0
  208. package/src/system/number-members.ts +29 -0
  209. package/src/system/number-parsing.ts +24 -4
  210. package/src/system/string-builder.ts +117 -0
  211. package/src/system/task-completion-source.ts +106 -0
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The key bucketing `groupBy`, `toLookup` and `join` share: the elements of a source
3
+ * under the equality .NET uses for a key (`EqualityComparer<K>.Default`, or a user
4
+ * comparer), in the order the keys are first seen, each bucket in source order.
5
+ */
6
+ import type { IEqualityComparer } from "../collections/default-comparer.ts";
7
+ import { HashCore } from "../collections/hash-core.ts";
8
+ import type { Grouping } from "./types.ts";
9
+
10
+ /**
11
+ * `comparer` as .NET's `Lookup` uses it: a `null` key hashes to 0 without asking the
12
+ * comparer, which may throw on it (`GetHashCode(string value) => value.Length`).
13
+ */
14
+ function nullSafe<K>(comparer: IEqualityComparer<K> | null | undefined) {
15
+ return comparer === null || comparer === undefined
16
+ ? comparer
17
+ : {
18
+ equals: (a: K, b: K) => comparer.equals(a, b),
19
+ getHashCode: (key: K) =>
20
+ key === null || key === undefined ? 0 : comparer.getHashCode(key),
21
+ };
22
+ }
23
+
24
+ /** Elements of `source` bucketed by `keySelector`; a `null` key is skipped when `skipNullKeys`. */
25
+ export function bucketsOf<T, K, V = T>(
26
+ source: Iterable<T>,
27
+ keySelector: (item: T) => K,
28
+ comparer: IEqualityComparer<K> | null | undefined,
29
+ elementSelector?: (item: T) => V,
30
+ skipNullKeys = false,
31
+ ): HashCore<K, V[]> {
32
+ const buckets = new HashCore<K, V[]>(nullSafe(comparer));
33
+
34
+ if (elementSelector === undefined && !skipNullKeys && comparer) {
35
+ return bucketAll(
36
+ source,
37
+ keySelector,
38
+ buckets as unknown as HashCore<K, T[]>,
39
+ ) as unknown as HashCore<K, V[]>;
40
+ }
41
+
42
+ for (const item of source) {
43
+ const key = keySelector(item);
44
+
45
+ if (skipNullKeys && (key === null || key === undefined)) {
46
+ continue;
47
+ }
48
+
49
+ buckets.append(
50
+ key,
51
+ elementSelector === undefined ? (item as unknown as V) : elementSelector(item),
52
+ );
53
+ }
54
+
55
+ return buckets;
56
+ }
57
+
58
+ function bucketAll<T, K>(
59
+ source: Iterable<T>,
60
+ keySelector: (item: T) => K,
61
+ buckets: HashCore<K, T[]>,
62
+ ): HashCore<K, T[]> {
63
+ for (const item of source) {
64
+ buckets.append(keySelector(item), item);
65
+ }
66
+
67
+ return buckets;
68
+ }
69
+
70
+ /** The groupings of `buckets`: an iterable bucket that also reads `.key`. */
71
+ export function* groupingsOf<K, T>(buckets: HashCore<K, T[]>): Generator<Grouping<K, T>> {
72
+ for (const [key, items] of buckets) {
73
+ // `Grouping` is `Iterable<T> & { key }`: the bucket array itself carries the key, so
74
+ // consumers can `for…of` the grouping and read `.key`.
75
+ yield Object.assign(items, { key }) as unknown as Grouping<K, T>;
76
+ }
77
+ }
@@ -71,6 +71,7 @@ export type {
71
71
  ThenByOp,
72
72
  PrependOp,
73
73
  ReverseOp,
74
+ DefaultIfEmptyOp,
74
75
  SelectManyOp,
75
76
  SelectOp,
76
77
  SkipOp,
@@ -87,6 +88,8 @@ export type {
87
88
  AverageInt64Term,
88
89
  ContainsTerm,
89
90
  CountTerm,
91
+ ElementAtTerm,
92
+ ElementAtOrDefaultTerm,
90
93
  FirstOrDefaultTerm,
91
94
  FirstTerm,
92
95
  LastOrDefaultTerm,
@@ -107,6 +110,9 @@ export type {
107
110
  ToListTerm,
108
111
  ToDictionaryTerm,
109
112
  ToHashSetTerm,
113
+ ToLookupTerm,
114
+ SequenceEqualTerm,
115
+ JoinOp,
110
116
  } from "./types.ts";
111
117
 
112
118
  export {
@@ -131,7 +137,9 @@ export {
131
137
  thenBy,
132
138
  thenByDescending,
133
139
  groupBy,
140
+ join,
134
141
  zip,
142
+ defaultIfEmpty,
135
143
  } from "./operators.ts";
136
144
 
137
145
  export {
@@ -141,6 +149,8 @@ export {
141
149
  toList,
142
150
  toDictionary,
143
151
  toHashSet,
152
+ toLookup,
153
+ sequenceEqual,
144
154
  first,
145
155
  firstOrDefault,
146
156
  last,
@@ -163,6 +173,8 @@ export {
163
173
  averageNullable,
164
174
  contains,
165
175
  aggregate,
176
+ elementAt,
177
+ elementAtOrDefault,
166
178
  } from "./terminals.ts";
167
179
 
168
180
  export {
@@ -177,5 +189,7 @@ export {
177
189
  sumInt64,
178
190
  } from "./int64-terminals.ts";
179
191
 
192
+ export { Lookup } from "./lookup.ts";
193
+
180
194
  export { averageDecimal, averageNullableDecimal, sumDecimal } from "./decimal-terminals.ts";
181
195
  export type { DecimalConstructor, DecimalValue } from "./decimal-terminals.ts";
@@ -1760,7 +1760,8 @@ export function linq(
1760
1760
  source: unknown,
1761
1761
  ...ops: (OperatorBase<any, any> | TerminalBase<any, any>)[]
1762
1762
  ): unknown {
1763
- const result = ops.reduce((acc, op) => op.apply(acc as Iterable<any>) as any, source as any);
1763
+ const start = typeof source === "string" ? codeUnitsOf(source) : source;
1764
+ const result = ops.reduce((acc, op) => op.apply(acc as Iterable<any>) as any, start as any);
1764
1765
  // Attach the original stage list to the materialized result so an
1765
1766
  // IQueryable-style consumer can introspect the chain. Skipped for
1766
1767
  // primitive results (terminals like `count` / `sum` return numbers
@@ -1814,3 +1815,24 @@ export function stages(...ops: Stage[]): Stage[] {
1814
1815
  return ops;
1815
1816
  }
1816
1817
  /* eslint-enable @typescript-eslint/no-explicit-any */
1818
+
1819
+ /**
1820
+ * The UTF-16 code units of `text`, read lazily: a C# string enumerates them,
1821
+ * where a JS string's own iterator yields code points, so a surrogate pair
1822
+ * would be one element (#695). No array is built, so an early-exit operator
1823
+ * (`first`, `any`, `take`) reads only the units it needs.
1824
+ */
1825
+ function codeUnitsOf(text: string): Iterable<string> {
1826
+ return {
1827
+ [Symbol.iterator]: () => {
1828
+ let index = 0;
1829
+
1830
+ return {
1831
+ next: (): IteratorResult<string> =>
1832
+ index < text.length
1833
+ ? { done: false, value: text[index++]! }
1834
+ : { done: true, value: undefined },
1835
+ };
1836
+ },
1837
+ };
1838
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * `System.Linq.ILookup<TKey, TElement>`, the result of `ToLookup`: the elements grouped by
3
+ * key, enumerated as groupings in first-seen key order. Keys are compared as `groupBy`
4
+ * compares them.
5
+ */
6
+ import type { HashCore } from "../collections/hash-core.ts";
7
+ import { groupingsOf } from "./grouping.ts";
8
+ import type { Grouping } from "./types.ts";
9
+
10
+ export class Lookup<K, T> implements Iterable<Grouping<K, T>> {
11
+ declare readonly _buckets: HashCore<K, T[]>;
12
+
13
+ constructor(buckets: HashCore<K, T[]>) {
14
+ Object.defineProperty(this, "_buckets", { value: buckets });
15
+ }
16
+
17
+ /** The number of distinct keys. */
18
+ get count(): number {
19
+ return this._buckets.size;
20
+ }
21
+
22
+ /** The elements under `key`, in source order; an empty sequence when the key is absent. */
23
+ get(key: K): Iterable<T> {
24
+ return this._buckets.get(key) ?? [];
25
+ }
26
+
27
+ contains(key: K): boolean {
28
+ return this._buckets.has(key);
29
+ }
30
+
31
+ [Symbol.iterator](): Iterator<Grouping<K, T>> {
32
+ return groupingsOf(this._buckets);
33
+ }
34
+ }
@@ -12,8 +12,11 @@
12
12
  * Short-circuit terminals (`first`, `take(n)`) stop iterating as soon
13
13
  * as their result is determined.
14
14
  */
15
+ import type { IEqualityComparer } from "../collections/default-comparer.ts";
16
+ import { HashCore } from "../collections/hash-core.ts";
15
17
  import { HashSet } from "../collections/hash-set.ts";
16
18
  import { compareKeys } from "./compare-keys.ts";
19
+ import { bucketsOf, groupingsOf } from "./grouping.ts";
17
20
  import type { QueryableMeta } from "./expr-tree.ts";
18
21
  import type {
19
22
  AppendOp,
@@ -22,10 +25,12 @@ import type {
22
25
  DistinctOp,
23
26
  ExceptOp,
24
27
  IntersectOp,
28
+ JoinOp,
25
29
  OrderByDescendingOp,
26
30
  OrderByOp,
27
31
  PrependOp,
28
32
  ReverseOp,
33
+ DefaultIfEmptyOp,
29
34
  SelectManyOp,
30
35
  SelectOp,
31
36
  SkipOp,
@@ -38,7 +43,6 @@ import type {
38
43
  WhereOp,
39
44
  ZipOp,
40
45
  GroupByOp,
41
- Grouping,
42
46
  } from "./types.ts";
43
47
 
44
48
  /**
@@ -203,12 +207,14 @@ export function skipWhile<T>(
203
207
  };
204
208
  }
205
209
 
206
- export function distinct<T>(): DistinctOp<T> {
210
+ /** `Distinct()` / `Distinct(comparer)`: the first of each element, equal by the comparer or by value. */
211
+ export function distinct<T>(comparer?: IEqualityComparer<T> | null): DistinctOp<T> {
207
212
  return {
208
213
  kind: "distinct",
214
+ comparer,
209
215
  apply: (source) => ({
210
216
  *[Symbol.iterator]() {
211
- const seen = new HashSet<T>();
217
+ const seen = new HashSet<T>(null, comparer);
212
218
  for (const item of source)
213
219
  if (!seen.contains(item)) {
214
220
  seen.add(item);
@@ -219,6 +225,31 @@ export function distinct<T>(): DistinctOp<T> {
219
225
  };
220
226
  }
221
227
 
228
+ /**
229
+ * `DefaultIfEmpty()` / `DefaultIfEmpty(defaultValue)`: without a `defaultValue` an empty
230
+ * source yields `null`; with one, the value. `T` comes from the surrounding `linq(...)` call.
231
+ */
232
+ export function defaultIfEmpty<T>(defaultValue?: null): DefaultIfEmptyOp<T, null>;
233
+ export function defaultIfEmpty<T>(defaultValue: NoInfer<T>): DefaultIfEmptyOp<T, T>;
234
+ export function defaultIfEmpty<T>(defaultValue: T | null = null): DefaultIfEmptyOp<T> {
235
+ return {
236
+ kind: "defaultIfEmpty",
237
+ defaultValue,
238
+ apply: (source) => ({
239
+ *[Symbol.iterator]() {
240
+ let any = false;
241
+
242
+ for (const item of source) {
243
+ any = true;
244
+ yield item;
245
+ }
246
+
247
+ if (!any) yield defaultValue as T;
248
+ },
249
+ }),
250
+ };
251
+ }
252
+
222
253
  export function distinctBy<T, K>(
223
254
  keySelector: (item: T) => K,
224
255
  queryable?: QueryableMeta,
@@ -425,35 +456,96 @@ export function thenByDescending<T, K>(
425
456
  /**
426
457
  * Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
427
458
  * distinct key — an Iterable carrying the shared key on `key` plus the
428
- * items in source order. Materializes the source to bucket items but
429
- * each enumeration of the result re-runs the bucketing.
459
+ * items in source order — in the order the keys are first seen. Keys are
460
+ * equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
461
+ * the source to bucket items but each enumeration of the result re-runs the bucketing.
430
462
  */
431
463
  export function groupBy<T, K>(
432
464
  keySelector: (item: T) => K,
465
+ comparer?: IEqualityComparer<NoInfer<K>> | null,
433
466
  queryable?: QueryableMeta,
434
467
  ): GroupByOp<T, K> {
435
468
  return {
436
469
  kind: "groupBy",
437
470
  keySelector,
471
+ comparer,
438
472
  queryable,
439
473
  apply: (source) => ({
474
+ // A generator, so the source is read on the first `next()`, as .NET's GroupBy does. With the
475
+ // default comparison the bucketing runs in this generator body, where V8 keeps it as fast as
476
+ // a native `Map` loop; a user comparer goes through the shared `bucketsOf`.
440
477
  *[Symbol.iterator]() {
441
- const buckets = new Map<K, T[]>();
478
+ if (comparer) {
479
+ yield* groupingsOf(bucketsOf(source, keySelector, comparer));
480
+
481
+ return;
482
+ }
483
+
484
+ const buckets = new HashCore<K, T[]>();
485
+ const entries = buckets._entries;
486
+
442
487
  for (const item of source) {
443
- const k = keySelector(item);
444
- let bucket = buckets.get(k);
488
+ const key = keySelector(item);
489
+
490
+ if (typeof key === "object" && key !== null) {
491
+ buckets.append(key, item);
492
+
493
+ continue;
494
+ }
495
+
496
+ const bucket = entries.get(key);
497
+
445
498
  if (bucket === undefined) {
446
- bucket = [];
447
- buckets.set(k, bucket);
499
+ entries.set(key, [item]);
500
+ } else {
501
+ bucket.push(item);
448
502
  }
449
- bucket.push(item);
450
503
  }
451
- for (const [key, items] of buckets) {
452
- // `Grouping` is `Iterable<T> & { key }` — wrap the bucket
453
- // array (Iterable) with the key field via Object.assign so
454
- // consumers can `for…of` the grouping AND read `.key`.
455
- const grouping = Object.assign(items, { key }) as unknown as Grouping<K, T>;
456
- yield grouping;
504
+
505
+ yield* groupingsOf(buckets);
506
+ },
507
+ }),
508
+ };
509
+ }
510
+
511
+ /**
512
+ * `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
513
+ * outer element, in order, one result per inner element of an equal key, in inner order.
514
+ * A `null` key matches nothing. The inner source is read once the first outer element is, and an
515
+ * inner with no key ends the join there.
516
+ */
517
+ export function join<T, U, K, R>(
518
+ inner: Iterable<U>,
519
+ outerKeySelector: (item: T) => K,
520
+ innerKeySelector: (item: U) => K,
521
+ resultSelector: (outer: T, inner: U) => R,
522
+ comparer?: IEqualityComparer<NoInfer<K>> | null,
523
+ ): JoinOp<T, U, K, R> {
524
+ return {
525
+ kind: "join",
526
+ inner,
527
+ outerKeySelector,
528
+ innerKeySelector,
529
+ resultSelector,
530
+ comparer,
531
+ apply: (source) => ({
532
+ *[Symbol.iterator]() {
533
+ let matches: HashCore<K, U[]> | undefined;
534
+
535
+ for (const outer of source) {
536
+ if (matches === undefined) {
537
+ matches = bucketsOf(inner, innerKeySelector, comparer, undefined, true);
538
+
539
+ // .NET reads one outer element, and stops there when the inner has no key.
540
+ if (matches.size === 0) return;
541
+ }
542
+
543
+ const key = outerKeySelector(outer);
544
+ const bucket = key === null || key === undefined ? undefined : matches.get(key);
545
+
546
+ if (bucket !== undefined) {
547
+ for (const item of bucket) yield resultSelector(outer, item);
548
+ }
457
549
  }
458
550
  },
459
551
  }),
@@ -9,13 +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";
15
+ import { throwArgumentNull, throwOOR } from "../collections/throw-helpers.ts";
14
16
  import { List } from "../collections/list.ts";
17
+ import { ArgumentOutOfRangeException } from "../exceptions/argument-out-of-range-exception.ts";
15
18
  import { InvalidOperationException } from "../exceptions/invalid-operation-exception.ts";
16
19
  import type { RuntimeType } from "../reflection/runtime-type.ts";
17
20
  import { compareKeys } from "./compare-keys.ts";
18
21
  import type { QueryableMeta } from "./expr-tree.ts";
22
+ import { bucketsOf } from "./grouping.ts";
23
+ import { Lookup } from "./lookup.ts";
19
24
  import type {
20
25
  AggregateTerm,
21
26
  AllTerm,
@@ -23,6 +28,8 @@ import type {
23
28
  AverageTerm,
24
29
  ContainsTerm,
25
30
  CountTerm,
31
+ ElementAtOrDefaultTerm,
32
+ ElementAtTerm,
26
33
  FirstOrDefaultTerm,
27
34
  FirstTerm,
28
35
  LastOrDefaultTerm,
@@ -33,11 +40,13 @@ import type {
33
40
  MinTerm,
34
41
  SingleOrDefaultTerm,
35
42
  SingleTerm,
43
+ SequenceEqualTerm,
36
44
  SumTerm,
37
45
  ToArrayTerm,
38
46
  ToDictionaryTerm,
39
47
  ToHashSetTerm,
40
48
  ToListTerm,
49
+ ToLookupTerm,
41
50
  ToMapTerm,
42
51
  ToSetTerm,
43
52
  } from "./types.ts";
@@ -191,6 +200,79 @@ export function firstOrDefault<T>(
191
200
  };
192
201
  }
193
202
 
203
+ /**
204
+ * `ElementAt(index)`.
205
+ *
206
+ * @throws ArgumentOutOfRangeException when `index` is negative or past the last element.
207
+ */
208
+ export function elementAt<T>(index: number): ElementAtTerm<T> {
209
+ return {
210
+ kind: "elementAt",
211
+ index,
212
+ apply: (source) => {
213
+ const found = elementOf(source, index);
214
+
215
+ if (found === undefined) return throwIndexOutOfRange(source);
216
+
217
+ return found.value;
218
+ },
219
+ };
220
+ }
221
+
222
+ /**
223
+ * `ElementAtOrDefault(index)`: without a `defaultValue` a miss is `null`; with one, the
224
+ * result is a `T`. `T` comes from the surrounding `linq(...)` call.
225
+ */
226
+ export function elementAtOrDefault<T>(
227
+ index: number,
228
+ defaultValue?: null,
229
+ ): ElementAtOrDefaultTerm<T, null>;
230
+ export function elementAtOrDefault<T>(
231
+ index: number,
232
+ defaultValue: NoInfer<T>,
233
+ ): ElementAtOrDefaultTerm<T, T>;
234
+ export function elementAtOrDefault<T>(
235
+ index: number,
236
+ defaultValue: T | null = null,
237
+ ): ElementAtOrDefaultTerm<T> {
238
+ return {
239
+ kind: "elementAtOrDefault",
240
+ index,
241
+ defaultValue,
242
+ apply: (source) => {
243
+ const found = elementOf(source, index);
244
+
245
+ return found === undefined ? defaultValue : found.value;
246
+ },
247
+ };
248
+ }
249
+
250
+ /**
251
+ * An index outside the source. An array or a `List` (`IList<T>` in .NET) reports the
252
+ * collection message, any other sequence the generic one.
253
+ */
254
+ function throwIndexOutOfRange(source: Iterable<unknown>): never {
255
+ if (Array.isArray(source) || source instanceof List) {
256
+ return throwOOR();
257
+ }
258
+
259
+ throw new ArgumentOutOfRangeException("index");
260
+ }
261
+
262
+ /** The element at `index`, boxed so an element that is `undefined` or `null` is told from a miss. */
263
+ function elementOf<T>(source: Iterable<T>, index: number): { readonly value: T } | undefined {
264
+ if (index < 0) return undefined;
265
+
266
+ let position = 0;
267
+
268
+ for (const item of source) {
269
+ if (position === index) return { value: item };
270
+ position++;
271
+ }
272
+
273
+ return undefined;
274
+ }
275
+
194
276
  export function last<T>(predicate?: (item: T) => boolean, queryable?: QueryableMeta): LastTerm<T> {
195
277
  return {
196
278
  kind: "last",
@@ -525,17 +607,94 @@ export function averageNullable<T>(
525
607
  };
526
608
  }
527
609
 
528
- 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> {
529
618
  return {
530
619
  kind: "contains",
531
620
  value,
621
+ comparer,
532
622
  apply: (source) => {
533
- 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;
534
631
  return false;
535
632
  },
536
633
  };
537
634
  }
538
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
+
539
698
  export function aggregate<T, A>(seed: A, accumulator: (acc: A, item: T) => A): AggregateTerm<T, A> {
540
699
  return {
541
700
  kind: "aggregate",