metano-runtime 3.0.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/system/char-members.d.ts +23 -0
- package/dist/system/char-members.js +46 -0
- package/dist/system/collections/hash-core.d.ts +5 -0
- package/dist/system/collections/hash-core.js +24 -0
- package/dist/system/collections/helpers.d.ts +16 -3
- package/dist/system/collections/helpers.js +28 -2
- package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist/system/collections/immutable-list-builder-core.js +11 -0
- package/dist/system/collections/immutable-list-core.d.ts +8 -0
- package/dist/system/collections/immutable-list-core.js +19 -1
- package/dist/system/collections/immutable-storage.d.ts +2 -0
- package/dist/system/collections/immutable-storage.js +6 -1
- package/dist/system/collections/interfaces.d.ts +13 -0
- package/dist/system/collections/list.d.ts +3 -0
- package/dist/system/collections/list.js +7 -0
- package/dist/system/collections/read-only-list.d.ts +11 -0
- package/dist/system/collections/read-only-list.js +26 -1
- package/dist/system/collections/sorted-list.d.ts +5 -0
- package/dist/system/collections/sorted-list.js +12 -1
- package/dist/system/comparable-compare-to.d.ts +12 -0
- package/dist/system/comparable-compare-to.js +20 -0
- package/dist/system/decimal-parsing.d.ts +12 -0
- package/dist/system/decimal-parsing.js +86 -0
- package/dist/system/exceptions/index.d.ts +1 -0
- package/dist/system/exceptions/index.js +1 -0
- package/dist/system/exceptions/throw-error.d.ts +7 -0
- package/dist/system/exceptions/throw-error.js +9 -0
- package/dist/system/hash-code.d.ts +2 -1
- package/dist/system/hash-code.js +5 -2
- package/dist/system/index.d.ts +3 -0
- package/dist/system/index.js +3 -0
- package/dist/system/int64-math.js +2 -1
- package/dist/system/int64.d.ts +6 -0
- package/dist/system/int64.js +18 -9
- package/dist/system/integer-division.d.ts +12 -4
- package/dist/system/integer-division.js +19 -2
- package/dist/system/json/index.d.ts +1 -1
- package/dist/system/json/json-serializer.d.ts +12 -1
- package/dist/system/json/json-serializer.js +29 -0
- package/dist/system/json/json-text-writer.d.ts +24 -0
- package/dist/system/json/json-text-writer.js +207 -0
- package/dist/system/json/number-from-string.js +20 -6
- package/dist/system/json/numeric-converters.js +41 -0
- package/dist/system/json/ordinal-ignore-case.d.ts +2 -0
- package/dist/system/json/ordinal-ignore-case.js +1 -1
- package/dist/system/json/serializer-context.d.ts +13 -7
- package/dist/system/json/serializer-context.js +23 -11
- package/dist/system/json/types.d.ts +20 -0
- package/dist/system/linq/grouping.d.ts +12 -0
- package/dist/system/linq/grouping.js +42 -0
- package/dist/system/linq/index.d.ts +4 -3
- package/dist/system/linq/index.js +3 -2
- package/dist/system/linq/linq.js +20 -1
- package/dist/system/linq/lookup.d.ts +17 -0
- package/dist/system/linq/lookup.js +20 -0
- package/dist/system/linq/operators.d.ts +12 -4
- package/dist/system/linq/operators.js +61 -15
- package/dist/system/linq/terminals.d.ts +29 -2
- package/dist/system/linq/terminals.js +71 -3
- package/dist/system/linq/types.d.ts +27 -4
- package/dist/system/math.d.ts +27 -0
- package/dist/system/math.js +45 -0
- package/dist/system/min-max.d.ts +6 -0
- package/dist/system/min-max.js +10 -0
- package/dist/system/number-parsing.d.ts +16 -4
- package/dist/system/number-parsing.js +21 -4
- package/dist/system/string-comparison.d.ts +56 -0
- package/dist/system/string-comparison.js +225 -0
- package/dist/system/string-hash-code.d.ts +14 -0
- package/dist/system/string-hash-code.js +37 -0
- package/dist/system/string-members.d.ts +5 -4
- package/dist/system/string-members.js +12 -11
- package/dist-unchecked/system/char-members.d.ts +23 -0
- package/dist-unchecked/system/char-members.js +46 -0
- package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
- package/dist-unchecked/system/collections/hash-core.js +24 -0
- package/dist-unchecked/system/collections/helpers.d.ts +16 -3
- package/dist-unchecked/system/collections/helpers.js +28 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
- package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
- package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
- package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
- package/dist-unchecked/system/collections/immutable-storage.js +6 -1
- package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
- package/dist-unchecked/system/collections/list.d.ts +3 -0
- package/dist-unchecked/system/collections/list.js +7 -0
- package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
- package/dist-unchecked/system/collections/read-only-list.js +26 -1
- package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
- package/dist-unchecked/system/collections/sorted-list.js +12 -1
- package/dist-unchecked/system/comparable-compare-to.d.ts +12 -0
- package/dist-unchecked/system/comparable-compare-to.js +20 -0
- package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
- package/dist-unchecked/system/decimal-parsing.js +86 -0
- package/dist-unchecked/system/exceptions/index.d.ts +1 -0
- package/dist-unchecked/system/exceptions/index.js +1 -0
- package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
- package/dist-unchecked/system/exceptions/throw-error.js +9 -0
- package/dist-unchecked/system/hash-code.d.ts +2 -1
- package/dist-unchecked/system/hash-code.js +5 -2
- package/dist-unchecked/system/index.d.ts +3 -0
- package/dist-unchecked/system/index.js +3 -0
- package/dist-unchecked/system/int64-math.js +2 -1
- package/dist-unchecked/system/int64.d.ts +6 -0
- package/dist-unchecked/system/int64.js +18 -9
- package/dist-unchecked/system/integer-division.d.ts +12 -4
- package/dist-unchecked/system/integer-division.js +19 -2
- package/dist-unchecked/system/json/index.d.ts +1 -1
- package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
- package/dist-unchecked/system/json/json-serializer.js +29 -0
- package/dist-unchecked/system/json/json-text-writer.d.ts +24 -0
- package/dist-unchecked/system/json/json-text-writer.js +207 -0
- package/dist-unchecked/system/json/number-from-string.js +20 -6
- package/dist-unchecked/system/json/numeric-converters.js +41 -0
- package/dist-unchecked/system/json/ordinal-ignore-case.d.ts +2 -0
- package/dist-unchecked/system/json/ordinal-ignore-case.js +1 -1
- package/dist-unchecked/system/json/serializer-context.d.ts +13 -7
- package/dist-unchecked/system/json/serializer-context.js +23 -11
- package/dist-unchecked/system/json/types.d.ts +20 -0
- package/dist-unchecked/system/linq/grouping.d.ts +12 -0
- package/dist-unchecked/system/linq/grouping.js +42 -0
- package/dist-unchecked/system/linq/index.d.ts +4 -3
- package/dist-unchecked/system/linq/index.js +3 -2
- package/dist-unchecked/system/linq/linq.js +20 -1
- package/dist-unchecked/system/linq/lookup.d.ts +17 -0
- package/dist-unchecked/system/linq/lookup.js +20 -0
- package/dist-unchecked/system/linq/operators.d.ts +12 -4
- package/dist-unchecked/system/linq/operators.js +61 -15
- package/dist-unchecked/system/linq/terminals.d.ts +29 -2
- package/dist-unchecked/system/linq/terminals.js +71 -3
- package/dist-unchecked/system/linq/types.d.ts +27 -4
- package/dist-unchecked/system/math.d.ts +27 -0
- package/dist-unchecked/system/math.js +45 -0
- package/dist-unchecked/system/min-max.d.ts +6 -0
- package/dist-unchecked/system/min-max.js +10 -0
- package/dist-unchecked/system/number-parsing.d.ts +16 -4
- package/dist-unchecked/system/number-parsing.js +21 -4
- package/dist-unchecked/system/string-comparison.d.ts +56 -0
- package/dist-unchecked/system/string-comparison.js +225 -0
- package/dist-unchecked/system/string-hash-code.d.ts +14 -0
- package/dist-unchecked/system/string-hash-code.js +37 -0
- package/dist-unchecked/system/string-members.d.ts +5 -4
- package/dist-unchecked/system/string-members.js +12 -11
- package/package.json +1 -1
- package/src/system/char-members.ts +57 -0
- package/src/system/collections/hash-core.ts +31 -0
- package/src/system/collections/helpers.ts +39 -4
- package/src/system/collections/immutable-list-builder-core.ts +16 -2
- package/src/system/collections/immutable-list-core.ts +23 -1
- package/src/system/collections/immutable-storage.ts +8 -1
- package/src/system/collections/interfaces.ts +16 -0
- package/src/system/collections/list.ts +9 -0
- package/src/system/collections/read-only-list.ts +35 -1
- package/src/system/collections/sorted-list.ts +15 -1
- package/src/system/comparable-compare-to.ts +23 -0
- package/src/system/decimal-parsing.ts +109 -0
- package/src/system/exceptions/index.ts +1 -0
- package/src/system/exceptions/throw-error.ts +9 -0
- package/src/system/hash-code.ts +7 -2
- package/src/system/index.ts +3 -0
- package/src/system/int64-math.ts +2 -1
- package/src/system/int64.ts +27 -10
- package/src/system/integer-division.ts +43 -6
- package/src/system/json/index.ts +1 -0
- package/src/system/json/json-serializer.ts +42 -0
- package/src/system/json/json-text-writer.ts +282 -0
- package/src/system/json/number-from-string.ts +26 -6
- package/src/system/json/numeric-converters.ts +55 -0
- package/src/system/json/ordinal-ignore-case.ts +1 -1
- package/src/system/json/serializer-context.ts +28 -11
- package/src/system/json/types.ts +21 -0
- package/src/system/linq/grouping.ts +77 -0
- package/src/system/linq/index.ts +8 -0
- package/src/system/linq/linq.ts +23 -1
- package/src/system/linq/lookup.ts +34 -0
- package/src/system/linq/operators.ts +78 -15
- package/src/system/linq/terminals.ts +85 -3
- package/src/system/linq/types.ts +32 -0
- package/src/system/math.ts +63 -0
- package/src/system/min-max.ts +11 -0
- package/src/system/number-parsing.ts +24 -4
- package/src/system/string-comparison.ts +299 -0
- package/src/system/string-hash-code.ts +44 -0
- package/src/system/string-members.ts +12 -11
package/src/system/linq/linq.ts
CHANGED
|
@@ -1760,7 +1760,8 @@ export function linq(
|
|
|
1760
1760
|
source: unknown,
|
|
1761
1761
|
...ops: (OperatorBase<any, any> | TerminalBase<any, any>)[]
|
|
1762
1762
|
): unknown {
|
|
1763
|
-
const
|
|
1763
|
+
const start = typeof source === "string" ? codeUnitsOf(source) : source;
|
|
1764
|
+
const result = ops.reduce((acc, op) => op.apply(acc as Iterable<any>) as any, start as any);
|
|
1764
1765
|
// Attach the original stage list to the materialized result so an
|
|
1765
1766
|
// IQueryable-style consumer can introspect the chain. Skipped for
|
|
1766
1767
|
// primitive results (terminals like `count` / `sum` return numbers
|
|
@@ -1814,3 +1815,24 @@ export function stages(...ops: Stage[]): Stage[] {
|
|
|
1814
1815
|
return ops;
|
|
1815
1816
|
}
|
|
1816
1817
|
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1818
|
+
|
|
1819
|
+
/**
|
|
1820
|
+
* The UTF-16 code units of `text`, read lazily: a C# string enumerates them,
|
|
1821
|
+
* where a JS string's own iterator yields code points, so a surrogate pair
|
|
1822
|
+
* would be one element (#695). No array is built, so an early-exit operator
|
|
1823
|
+
* (`first`, `any`, `take`) reads only the units it needs.
|
|
1824
|
+
*/
|
|
1825
|
+
function codeUnitsOf(text: string): Iterable<string> {
|
|
1826
|
+
return {
|
|
1827
|
+
[Symbol.iterator]: () => {
|
|
1828
|
+
let index = 0;
|
|
1829
|
+
|
|
1830
|
+
return {
|
|
1831
|
+
next: (): IteratorResult<string> =>
|
|
1832
|
+
index < text.length
|
|
1833
|
+
? { done: false, value: text[index++]! }
|
|
1834
|
+
: { done: true, value: undefined },
|
|
1835
|
+
};
|
|
1836
|
+
},
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `System.Linq.ILookup<TKey, TElement>`, the result of `ToLookup`: the elements grouped by
|
|
3
|
+
* key, enumerated as groupings in first-seen key order. Keys are compared as `groupBy`
|
|
4
|
+
* compares them.
|
|
5
|
+
*/
|
|
6
|
+
import type { HashCore } from "../collections/hash-core.ts";
|
|
7
|
+
import { groupingsOf } from "./grouping.ts";
|
|
8
|
+
import type { Grouping } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
export class Lookup<K, T> implements Iterable<Grouping<K, T>> {
|
|
11
|
+
declare readonly _buckets: HashCore<K, T[]>;
|
|
12
|
+
|
|
13
|
+
constructor(buckets: HashCore<K, T[]>) {
|
|
14
|
+
Object.defineProperty(this, "_buckets", { value: buckets });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The number of distinct keys. */
|
|
18
|
+
get count(): number {
|
|
19
|
+
return this._buckets.size;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The elements under `key`, in source order; an empty sequence when the key is absent. */
|
|
23
|
+
get(key: K): Iterable<T> {
|
|
24
|
+
return this._buckets.get(key) ?? [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
contains(key: K): boolean {
|
|
28
|
+
return this._buckets.has(key);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
[Symbol.iterator](): Iterator<Grouping<K, T>> {
|
|
32
|
+
return groupingsOf(this._buckets);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -13,8 +13,10 @@
|
|
|
13
13
|
* as their result is determined.
|
|
14
14
|
*/
|
|
15
15
|
import type { IEqualityComparer } from "../collections/default-comparer.ts";
|
|
16
|
+
import { HashCore } from "../collections/hash-core.ts";
|
|
16
17
|
import { HashSet } from "../collections/hash-set.ts";
|
|
17
18
|
import { compareKeys } from "./compare-keys.ts";
|
|
19
|
+
import { bucketsOf, groupingsOf } from "./grouping.ts";
|
|
18
20
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
19
21
|
import type {
|
|
20
22
|
AppendOp,
|
|
@@ -23,6 +25,7 @@ import type {
|
|
|
23
25
|
DistinctOp,
|
|
24
26
|
ExceptOp,
|
|
25
27
|
IntersectOp,
|
|
28
|
+
JoinOp,
|
|
26
29
|
OrderByDescendingOp,
|
|
27
30
|
OrderByOp,
|
|
28
31
|
PrependOp,
|
|
@@ -40,7 +43,6 @@ import type {
|
|
|
40
43
|
WhereOp,
|
|
41
44
|
ZipOp,
|
|
42
45
|
GroupByOp,
|
|
43
|
-
Grouping,
|
|
44
46
|
} from "./types.ts";
|
|
45
47
|
|
|
46
48
|
/**
|
|
@@ -454,35 +456,96 @@ export function thenByDescending<T, K>(
|
|
|
454
456
|
/**
|
|
455
457
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
456
458
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
457
|
-
* items in source order
|
|
458
|
-
*
|
|
459
|
+
* items in source order — in the order the keys are first seen. Keys are
|
|
460
|
+
* equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
|
|
461
|
+
* the source to bucket items but each enumeration of the result re-runs the bucketing.
|
|
459
462
|
*/
|
|
460
463
|
export function groupBy<T, K>(
|
|
461
464
|
keySelector: (item: T) => K,
|
|
465
|
+
comparer?: IEqualityComparer<NoInfer<K>> | null,
|
|
462
466
|
queryable?: QueryableMeta,
|
|
463
467
|
): GroupByOp<T, K> {
|
|
464
468
|
return {
|
|
465
469
|
kind: "groupBy",
|
|
466
470
|
keySelector,
|
|
471
|
+
comparer,
|
|
467
472
|
queryable,
|
|
468
473
|
apply: (source) => ({
|
|
474
|
+
// A generator, so the source is read on the first `next()`, as .NET's GroupBy does. With the
|
|
475
|
+
// default comparison the bucketing runs in this generator body, where V8 keeps it as fast as
|
|
476
|
+
// a native `Map` loop; a user comparer goes through the shared `bucketsOf`.
|
|
469
477
|
*[Symbol.iterator]() {
|
|
470
|
-
|
|
478
|
+
if (comparer) {
|
|
479
|
+
yield* groupingsOf(bucketsOf(source, keySelector, comparer));
|
|
480
|
+
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const buckets = new HashCore<K, T[]>();
|
|
485
|
+
const entries = buckets._entries;
|
|
486
|
+
|
|
471
487
|
for (const item of source) {
|
|
472
|
-
const
|
|
473
|
-
|
|
488
|
+
const key = keySelector(item);
|
|
489
|
+
|
|
490
|
+
if (typeof key === "object" && key !== null) {
|
|
491
|
+
buckets.append(key, item);
|
|
492
|
+
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const bucket = entries.get(key);
|
|
497
|
+
|
|
474
498
|
if (bucket === undefined) {
|
|
475
|
-
|
|
476
|
-
|
|
499
|
+
entries.set(key, [item]);
|
|
500
|
+
} else {
|
|
501
|
+
bucket.push(item);
|
|
477
502
|
}
|
|
478
|
-
bucket.push(item);
|
|
479
503
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
504
|
+
|
|
505
|
+
yield* groupingsOf(buckets);
|
|
506
|
+
},
|
|
507
|
+
}),
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
|
|
513
|
+
* outer element, in order, one result per inner element of an equal key, in inner order.
|
|
514
|
+
* A `null` key matches nothing. The inner source is read once the first outer element is, and an
|
|
515
|
+
* inner with no key ends the join there.
|
|
516
|
+
*/
|
|
517
|
+
export function join<T, U, K, R>(
|
|
518
|
+
inner: Iterable<U>,
|
|
519
|
+
outerKeySelector: (item: T) => K,
|
|
520
|
+
innerKeySelector: (item: U) => K,
|
|
521
|
+
resultSelector: (outer: T, inner: U) => R,
|
|
522
|
+
comparer?: IEqualityComparer<NoInfer<K>> | null,
|
|
523
|
+
): JoinOp<T, U, K, R> {
|
|
524
|
+
return {
|
|
525
|
+
kind: "join",
|
|
526
|
+
inner,
|
|
527
|
+
outerKeySelector,
|
|
528
|
+
innerKeySelector,
|
|
529
|
+
resultSelector,
|
|
530
|
+
comparer,
|
|
531
|
+
apply: (source) => ({
|
|
532
|
+
*[Symbol.iterator]() {
|
|
533
|
+
let matches: HashCore<K, U[]> | undefined;
|
|
534
|
+
|
|
535
|
+
for (const outer of source) {
|
|
536
|
+
if (matches === undefined) {
|
|
537
|
+
matches = bucketsOf(inner, innerKeySelector, comparer, undefined, true);
|
|
538
|
+
|
|
539
|
+
// .NET reads one outer element, and stops there when the inner has no key.
|
|
540
|
+
if (matches.size === 0) return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const key = outerKeySelector(outer);
|
|
544
|
+
const bucket = key === null || key === undefined ? undefined : matches.get(key);
|
|
545
|
+
|
|
546
|
+
if (bucket !== undefined) {
|
|
547
|
+
for (const item of bucket) yield resultSelector(outer, item);
|
|
548
|
+
}
|
|
486
549
|
}
|
|
487
550
|
},
|
|
488
551
|
}),
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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",
|
package/src/system/linq/types.ts
CHANGED
|
@@ -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>;
|
package/src/system/math.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
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";
|
|
15
|
+
import { throwMinGreaterThanMax } from "./min-max.ts";
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
@@ -180,3 +182,64 @@ function assertMode(mode: number): asserts mode is MidpointRounding {
|
|
|
180
182
|
);
|
|
181
183
|
}
|
|
182
184
|
}
|
|
185
|
+
|
|
186
|
+
const INT32_MIN = -2147483648;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
|
|
190
|
+
* `int.MinValue`, whose magnitude does not fit `int`.
|
|
191
|
+
*
|
|
192
|
+
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
193
|
+
*/
|
|
194
|
+
export function absInt32(value: number): number {
|
|
195
|
+
if (value === INT32_MIN) {
|
|
196
|
+
throw new OverflowException(
|
|
197
|
+
"Negating the minimum value of a twos complement number is invalid.",
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return Math.abs(value);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
206
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
207
|
+
* stays `NaN`, as in .NET.
|
|
208
|
+
*
|
|
209
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
210
|
+
*/
|
|
211
|
+
export function clampNumber(value: number, min: number, max: number): number {
|
|
212
|
+
if (min > max) {
|
|
213
|
+
throwMinGreaterThanMax(min, max);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (value < min) {
|
|
217
|
+
return min;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return value > max ? max : value;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Structural view of a `decimal.js` `Decimal`, the only operations {@link clampDecimal} uses. */
|
|
224
|
+
export interface DecimalClampable<D> {
|
|
225
|
+
lt(other: D): boolean;
|
|
226
|
+
gt(other: D): boolean;
|
|
227
|
+
toString(): string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
232
|
+
*
|
|
233
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
234
|
+
*/
|
|
235
|
+
export function clampDecimal<D extends DecimalClampable<D>>(value: D, min: D, max: D): D {
|
|
236
|
+
if (min.gt(max)) {
|
|
237
|
+
throwMinGreaterThanMax(min, max);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (value.lt(min)) {
|
|
241
|
+
return min;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return value.gt(max) ? max : value;
|
|
245
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ArgumentException } from "./exceptions/argument-exception.ts";
|
|
2
|
+
import { formatText } from "./default-text.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
6
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
7
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
8
|
+
*/
|
|
9
|
+
export function throwMinGreaterThanMax(min: unknown, max: unknown): never {
|
|
10
|
+
throw new ArgumentException(`'${formatText(min)}' cannot be greater than ${formatText(max)}.`);
|
|
11
|
+
}
|
|
@@ -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)
|
|
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)
|
|
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
|
*
|