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
|
@@ -1,19 +1,7 @@
|
|
|
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
|
-
*/
|
|
1
|
+
import { HashCore } from "../collections/hash-core.js";
|
|
15
2
|
import { HashSet } from "../collections/hash-set.js";
|
|
16
3
|
import { compareKeys } from "./compare-keys.js";
|
|
4
|
+
import { bucketsOf, groupingsOf } from "./grouping.js";
|
|
17
5
|
class OrderedIter {
|
|
18
6
|
source;
|
|
19
7
|
comparers;
|
|
@@ -154,12 +142,14 @@ export function skipWhile(predicate, queryable) {
|
|
|
154
142
|
}),
|
|
155
143
|
};
|
|
156
144
|
}
|
|
157
|
-
|
|
145
|
+
/** `Distinct()` / `Distinct(comparer)`: the first of each element, equal by the comparer or by value. */
|
|
146
|
+
export function distinct(comparer) {
|
|
158
147
|
return {
|
|
159
148
|
kind: "distinct",
|
|
149
|
+
comparer,
|
|
160
150
|
apply: (source) => ({
|
|
161
151
|
*[Symbol.iterator]() {
|
|
162
|
-
const seen = new HashSet();
|
|
152
|
+
const seen = new HashSet(null, comparer);
|
|
163
153
|
for (const item of source)
|
|
164
154
|
if (!seen.contains(item)) {
|
|
165
155
|
seen.add(item);
|
|
@@ -169,6 +159,23 @@ export function distinct() {
|
|
|
169
159
|
}),
|
|
170
160
|
};
|
|
171
161
|
}
|
|
162
|
+
export function defaultIfEmpty(defaultValue = null) {
|
|
163
|
+
return {
|
|
164
|
+
kind: "defaultIfEmpty",
|
|
165
|
+
defaultValue,
|
|
166
|
+
apply: (source) => ({
|
|
167
|
+
*[Symbol.iterator]() {
|
|
168
|
+
let any = false;
|
|
169
|
+
for (const item of source) {
|
|
170
|
+
any = true;
|
|
171
|
+
yield item;
|
|
172
|
+
}
|
|
173
|
+
if (!any)
|
|
174
|
+
yield defaultValue;
|
|
175
|
+
},
|
|
176
|
+
}),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
172
179
|
export function distinctBy(keySelector, queryable) {
|
|
173
180
|
return {
|
|
174
181
|
kind: "distinctBy",
|
|
@@ -350,32 +357,76 @@ export function thenByDescending(keySelector, queryable) {
|
|
|
350
357
|
/**
|
|
351
358
|
* Groups elements by a key. Yields one <see cref="Grouping{K, T}"/> per
|
|
352
359
|
* distinct key — an Iterable carrying the shared key on `key` plus the
|
|
353
|
-
* items in source order
|
|
354
|
-
*
|
|
360
|
+
* items in source order — in the order the keys are first seen. Keys are
|
|
361
|
+
* equal as .NET's `EqualityComparer<K>.Default` (or `comparer`) says. Materializes
|
|
362
|
+
* the source to bucket items but each enumeration of the result re-runs the bucketing.
|
|
355
363
|
*/
|
|
356
|
-
export function groupBy(keySelector, queryable) {
|
|
364
|
+
export function groupBy(keySelector, comparer, queryable) {
|
|
357
365
|
return {
|
|
358
366
|
kind: "groupBy",
|
|
359
367
|
keySelector,
|
|
368
|
+
comparer,
|
|
360
369
|
queryable,
|
|
361
370
|
apply: (source) => ({
|
|
371
|
+
// A generator, so the source is read on the first `next()`, as .NET's GroupBy does. With the
|
|
372
|
+
// default comparison the bucketing runs in this generator body, where V8 keeps it as fast as
|
|
373
|
+
// a native `Map` loop; a user comparer goes through the shared `bucketsOf`.
|
|
362
374
|
*[Symbol.iterator]() {
|
|
363
|
-
|
|
375
|
+
if (comparer) {
|
|
376
|
+
yield* groupingsOf(bucketsOf(source, keySelector, comparer));
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const buckets = new HashCore();
|
|
380
|
+
const entries = buckets._entries;
|
|
364
381
|
for (const item of source) {
|
|
365
|
-
const
|
|
366
|
-
|
|
382
|
+
const key = keySelector(item);
|
|
383
|
+
if (typeof key === "object" && key !== null) {
|
|
384
|
+
buckets.append(key, item);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const bucket = entries.get(key);
|
|
367
388
|
if (bucket === undefined) {
|
|
368
|
-
|
|
369
|
-
|
|
389
|
+
entries.set(key, [item]);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
bucket.push(item);
|
|
370
393
|
}
|
|
371
|
-
bucket.push(item);
|
|
372
394
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
395
|
+
yield* groupingsOf(buckets);
|
|
396
|
+
},
|
|
397
|
+
}),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`: for each
|
|
402
|
+
* outer element, in order, one result per inner element of an equal key, in inner order.
|
|
403
|
+
* A `null` key matches nothing. The inner source is read once the first outer element is, and an
|
|
404
|
+
* inner with no key ends the join there.
|
|
405
|
+
*/
|
|
406
|
+
export function join(inner, outerKeySelector, innerKeySelector, resultSelector, comparer) {
|
|
407
|
+
return {
|
|
408
|
+
kind: "join",
|
|
409
|
+
inner,
|
|
410
|
+
outerKeySelector,
|
|
411
|
+
innerKeySelector,
|
|
412
|
+
resultSelector,
|
|
413
|
+
comparer,
|
|
414
|
+
apply: (source) => ({
|
|
415
|
+
*[Symbol.iterator]() {
|
|
416
|
+
let matches;
|
|
417
|
+
for (const outer of source) {
|
|
418
|
+
if (matches === undefined) {
|
|
419
|
+
matches = bucketsOf(inner, innerKeySelector, comparer, undefined, true);
|
|
420
|
+
// .NET reads one outer element, and stops there when the inner has no key.
|
|
421
|
+
if (matches.size === 0)
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const key = outerKeySelector(outer);
|
|
425
|
+
const bucket = key === null || key === undefined ? undefined : matches.get(key);
|
|
426
|
+
if (bucket !== undefined) {
|
|
427
|
+
for (const item of bucket)
|
|
428
|
+
yield resultSelector(outer, item);
|
|
429
|
+
}
|
|
379
430
|
}
|
|
380
431
|
},
|
|
381
432
|
}),
|
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal factories — each returns a tagged descriptor whose `apply`
|
|
3
|
+
* walks the source and produces a concrete value (array, scalar,
|
|
4
|
+
* boolean). Same descriptor shape as composition operators so an
|
|
5
|
+
* IQueryable provider can switch over the terminal kind exhaustively
|
|
6
|
+
* (e.g. translate `count` to `SELECT COUNT(*)`, `sum` to
|
|
7
|
+
* `SELECT SUM(<selector>)`, etc.).
|
|
8
|
+
*
|
|
9
|
+
* Short-circuit terminals (`first`, `any`, `contains`) stop iterating
|
|
10
|
+
* as soon as the answer is known.
|
|
11
|
+
*/
|
|
12
|
+
import { type IEqualityComparer } from "../collections/default-comparer.ts";
|
|
1
13
|
import type { RuntimeType } from "../reflection/runtime-type.ts";
|
|
2
14
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
3
|
-
import type { AggregateTerm, AllTerm, AnyTerm, AverageTerm, ContainsTerm, CountTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxByTerm, MaxTerm, MinByTerm, MinTerm, SingleOrDefaultTerm, SingleTerm, SumTerm, ToArrayTerm, ToDictionaryTerm, ToHashSetTerm, ToListTerm, ToMapTerm, ToSetTerm } from "./types.ts";
|
|
15
|
+
import type { AggregateTerm, AllTerm, AnyTerm, AverageTerm, ContainsTerm, CountTerm, ElementAtOrDefaultTerm, ElementAtTerm, FirstOrDefaultTerm, FirstTerm, LastOrDefaultTerm, LastTerm, MaxByTerm, MaxTerm, MinByTerm, MinTerm, SingleOrDefaultTerm, SingleTerm, SequenceEqualTerm, SumTerm, ToArrayTerm, ToDictionaryTerm, ToHashSetTerm, ToListTerm, ToLookupTerm, ToMapTerm, ToSetTerm } from "./types.ts";
|
|
4
16
|
export declare function toArray<T>(): ToArrayTerm<T>;
|
|
5
17
|
export declare function toMap<T, K, V = T>(keySelector: (item: T) => K, valueSelector?: (item: T) => V, keyQueryable?: QueryableMeta, valueQueryable?: QueryableMeta): ToMapTerm<T, K, V>;
|
|
6
18
|
export declare function toSet<T>(): ToSetTerm<T>;
|
|
@@ -23,6 +35,18 @@ export declare function first<T>(predicate?: (item: T) => boolean, queryable?: Q
|
|
|
23
35
|
export declare function firstOrDefault<T>(predicate?: (item: T) => boolean, defaultValue?: null, queryable?: QueryableMeta): FirstOrDefaultTerm<T, null>;
|
|
24
36
|
export declare function firstOrDefault<T>(predicate: ((item: T) => boolean) | undefined, defaultValue: NoInfer<T>, queryable?: QueryableMeta): FirstOrDefaultTerm<T, T>;
|
|
25
37
|
export declare function firstOrDefault<T>(predicate: ((item: T) => boolean) | undefined, defaultValue: NoInfer<T> | null | undefined, queryable?: QueryableMeta): FirstOrDefaultTerm<T, T | null>;
|
|
38
|
+
/**
|
|
39
|
+
* `ElementAt(index)`.
|
|
40
|
+
*
|
|
41
|
+
* @throws ArgumentOutOfRangeException when `index` is negative or past the last element.
|
|
42
|
+
*/
|
|
43
|
+
export declare function elementAt<T>(index: number): ElementAtTerm<T>;
|
|
44
|
+
/**
|
|
45
|
+
* `ElementAtOrDefault(index)`: without a `defaultValue` a miss is `null`; with one, the
|
|
46
|
+
* result is a `T`. `T` comes from the surrounding `linq(...)` call.
|
|
47
|
+
*/
|
|
48
|
+
export declare function elementAtOrDefault<T>(index: number, defaultValue?: null): ElementAtOrDefaultTerm<T, null>;
|
|
49
|
+
export declare function elementAtOrDefault<T>(index: number, defaultValue: NoInfer<T>): ElementAtOrDefaultTerm<T, T>;
|
|
26
50
|
export declare function last<T>(predicate?: (item: T) => boolean, queryable?: QueryableMeta): LastTerm<T>;
|
|
27
51
|
/**
|
|
28
52
|
* `LastOrDefault`: without a `defaultValue` a miss is `null`; with one, the result is a `T`
|
|
@@ -96,5 +120,20 @@ export declare function maxByNullable<T, K>(keySelector: (item: T) => K, queryab
|
|
|
96
120
|
export declare function average<T>(selector?: (item: T) => number, queryable?: QueryableMeta): AverageTerm<T>;
|
|
97
121
|
/** `Average()` / `Average(selector)` over `int?`, `double?`, …: nulls skipped; `null` when none is left. */
|
|
98
122
|
export declare function averageNullable<T>(selector?: (item: T) => number | null, queryable?: QueryableMeta): AverageTerm<T, number | null>;
|
|
99
|
-
|
|
123
|
+
/**
|
|
124
|
+
* `Contains(value[, comparer])`: whether an element equals `value` by `comparer`, or, without
|
|
125
|
+
* one, by .NET's `EqualityComparer<T>.Default` (a record, `NaN` and a decimal by value).
|
|
126
|
+
*/
|
|
127
|
+
export declare function contains<T>(value: T, comparer?: IEqualityComparer<NoInfer<T>> | null): ContainsTerm<T>;
|
|
128
|
+
/**
|
|
129
|
+
* `SequenceEqual(second[, comparer])`: whether both sequences have the same length and equal
|
|
130
|
+
* elements in order, equal by `comparer` or by `EqualityComparer<T>.Default`.
|
|
131
|
+
*/
|
|
132
|
+
export declare function sequenceEqual<T>(second: Iterable<T>, comparer?: IEqualityComparer<NoInfer<T>> | null): SequenceEqualTerm<T>;
|
|
133
|
+
/**
|
|
134
|
+
* `ToLookup(keySelector[, elementSelector][, comparer])`: a `Lookup` of the elements by key.
|
|
135
|
+
* The second argument is the `elementSelector` function or, in the `(keySelector, comparer)`
|
|
136
|
+
* overload, the comparer object.
|
|
137
|
+
*/
|
|
138
|
+
export declare function toLookup<T, K, V = T>(keySelector: (item: T) => K, elementSelector?: ((item: T) => V) | IEqualityComparer<NoInfer<K>> | null, comparer?: IEqualityComparer<NoInfer<K>> | null): ToLookupTerm<T, K, V>;
|
|
100
139
|
export declare function aggregate<T, A>(seed: A, accumulator: (acc: A, item: T) => A): AggregateTerm<T, A>;
|
|
@@ -9,11 +9,16 @@
|
|
|
9
9
|
* Short-circuit terminals (`first`, `any`, `contains`) stop iterating
|
|
10
10
|
* as soon as the answer is known.
|
|
11
11
|
*/
|
|
12
|
+
import { elementEquals } from "../collections/default-comparer.js";
|
|
12
13
|
import { Dictionary } from "../collections/dictionary.js";
|
|
13
14
|
import { HashSet } from "../collections/hash-set.js";
|
|
15
|
+
import { throwArgumentNull, throwOOR } from "../collections/throw-helpers.js";
|
|
14
16
|
import { List } from "../collections/list.js";
|
|
17
|
+
import { ArgumentOutOfRangeException } from "../exceptions/argument-out-of-range-exception.js";
|
|
15
18
|
import { InvalidOperationException } from "../exceptions/invalid-operation-exception.js";
|
|
16
19
|
import { compareKeys } from "./compare-keys.js";
|
|
20
|
+
import { bucketsOf } from "./grouping.js";
|
|
21
|
+
import { Lookup } from "./lookup.js";
|
|
17
22
|
export function toArray() {
|
|
18
23
|
return {
|
|
19
24
|
kind: "toArray",
|
|
@@ -100,6 +105,56 @@ export function firstOrDefault(predicate, defaultValue = null, queryable) {
|
|
|
100
105
|
},
|
|
101
106
|
};
|
|
102
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* `ElementAt(index)`.
|
|
110
|
+
*
|
|
111
|
+
* @throws ArgumentOutOfRangeException when `index` is negative or past the last element.
|
|
112
|
+
*/
|
|
113
|
+
export function elementAt(index) {
|
|
114
|
+
return {
|
|
115
|
+
kind: "elementAt",
|
|
116
|
+
index,
|
|
117
|
+
apply: (source) => {
|
|
118
|
+
const found = elementOf(source, index);
|
|
119
|
+
if (found === undefined)
|
|
120
|
+
return throwIndexOutOfRange(source);
|
|
121
|
+
return found.value;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export function elementAtOrDefault(index, defaultValue = null) {
|
|
126
|
+
return {
|
|
127
|
+
kind: "elementAtOrDefault",
|
|
128
|
+
index,
|
|
129
|
+
defaultValue,
|
|
130
|
+
apply: (source) => {
|
|
131
|
+
const found = elementOf(source, index);
|
|
132
|
+
return found === undefined ? defaultValue : found.value;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* An index outside the source. An array or a `List` (`IList<T>` in .NET) reports the
|
|
138
|
+
* collection message, any other sequence the generic one.
|
|
139
|
+
*/
|
|
140
|
+
function throwIndexOutOfRange(source) {
|
|
141
|
+
if (Array.isArray(source) || source instanceof List) {
|
|
142
|
+
return throwOOR();
|
|
143
|
+
}
|
|
144
|
+
throw new ArgumentOutOfRangeException("index");
|
|
145
|
+
}
|
|
146
|
+
/** The element at `index`, boxed so an element that is `undefined` or `null` is told from a miss. */
|
|
147
|
+
function elementOf(source, index) {
|
|
148
|
+
if (index < 0)
|
|
149
|
+
return undefined;
|
|
150
|
+
let position = 0;
|
|
151
|
+
for (const item of source) {
|
|
152
|
+
if (position === index)
|
|
153
|
+
return { value: item };
|
|
154
|
+
position++;
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
103
158
|
export function last(predicate, queryable) {
|
|
104
159
|
return {
|
|
105
160
|
kind: "last",
|
|
@@ -347,18 +402,83 @@ export function averageNullable(selector, queryable) {
|
|
|
347
402
|
apply: (source) => averageOf(valuesOf(source, selector)),
|
|
348
403
|
};
|
|
349
404
|
}
|
|
350
|
-
|
|
405
|
+
/**
|
|
406
|
+
* `Contains(value[, comparer])`: whether an element equals `value` by `comparer`, or, without
|
|
407
|
+
* one, by .NET's `EqualityComparer<T>.Default` (a record, `NaN` and a decimal by value).
|
|
408
|
+
*/
|
|
409
|
+
export function contains(value, comparer) {
|
|
351
410
|
return {
|
|
352
411
|
kind: "contains",
|
|
353
412
|
value,
|
|
413
|
+
comparer,
|
|
354
414
|
apply: (source) => {
|
|
415
|
+
// A primitive other than NaN is equal to exactly what `===` says, with no comparer. `null`
|
|
416
|
+
// is left out: generated code can hold `undefined` where C# has null.
|
|
417
|
+
if (!comparer && value != null && typeof value !== "object" && !Number.isNaN(value)) {
|
|
418
|
+
for (const element of source)
|
|
419
|
+
if (element === value)
|
|
420
|
+
return true;
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
355
423
|
for (const element of source)
|
|
356
|
-
if (element
|
|
424
|
+
if (equalsBy(comparer, element, value))
|
|
357
425
|
return true;
|
|
358
426
|
return false;
|
|
359
427
|
},
|
|
360
428
|
};
|
|
361
429
|
}
|
|
430
|
+
/**
|
|
431
|
+
* `SequenceEqual(second[, comparer])`: whether both sequences have the same length and equal
|
|
432
|
+
* elements in order, equal by `comparer` or by `EqualityComparer<T>.Default`.
|
|
433
|
+
*/
|
|
434
|
+
export function sequenceEqual(second, comparer) {
|
|
435
|
+
return {
|
|
436
|
+
kind: "sequenceEqual",
|
|
437
|
+
second,
|
|
438
|
+
comparer,
|
|
439
|
+
apply: (source) => {
|
|
440
|
+
if (second === null || second === undefined)
|
|
441
|
+
throwArgumentNull("second");
|
|
442
|
+
const first = source[Symbol.iterator]();
|
|
443
|
+
const other = second[Symbol.iterator]();
|
|
444
|
+
try {
|
|
445
|
+
while (true) {
|
|
446
|
+
const a = first.next();
|
|
447
|
+
const b = other.next();
|
|
448
|
+
if (a.done || b.done)
|
|
449
|
+
return a.done === b.done;
|
|
450
|
+
if (!equalsBy(comparer, a.value, b.value))
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
finally {
|
|
455
|
+
first.return?.();
|
|
456
|
+
other.return?.();
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* `ToLookup(keySelector[, elementSelector][, comparer])`: a `Lookup` of the elements by key.
|
|
463
|
+
* The second argument is the `elementSelector` function or, in the `(keySelector, comparer)`
|
|
464
|
+
* overload, the comparer object.
|
|
465
|
+
*/
|
|
466
|
+
export function toLookup(keySelector, elementSelector, comparer) {
|
|
467
|
+
const selectsElement = typeof elementSelector === "function";
|
|
468
|
+
const keyComparer = selectsElement ? comparer : (elementSelector ?? comparer);
|
|
469
|
+
const select = selectsElement ? elementSelector : undefined;
|
|
470
|
+
return {
|
|
471
|
+
kind: "toLookup",
|
|
472
|
+
keySelector,
|
|
473
|
+
elementSelector: select,
|
|
474
|
+
comparer: keyComparer,
|
|
475
|
+
apply: (source) => new Lookup(bucketsOf(source, keySelector, keyComparer, select)),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
/** `comparer.Equals(a, b)`, or `EqualityComparer<T>.Default` without a comparer. */
|
|
479
|
+
function equalsBy(comparer, a, b) {
|
|
480
|
+
return comparer ? comparer.equals(a, b) : elementEquals(a, b);
|
|
481
|
+
}
|
|
362
482
|
export function aggregate(seed, accumulator) {
|
|
363
483
|
return {
|
|
364
484
|
kind: "aggregate",
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { IEqualityComparer } from "../collections/default-comparer.ts";
|
|
1
2
|
import type { Dictionary } from "../collections/dictionary.ts";
|
|
2
3
|
import type { HashSet } from "../collections/hash-set.ts";
|
|
3
4
|
import type { List } from "../collections/list.ts";
|
|
4
5
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
6
|
+
import type { Lookup } from "./lookup.ts";
|
|
5
7
|
/**
|
|
6
8
|
* Descriptor-based LINQ runtime — prototype for #20.
|
|
7
9
|
*
|
|
@@ -22,8 +24,8 @@ import type { QueryableMeta } from "./expr-tree.ts";
|
|
|
22
24
|
* Bundlers still tree-shake every unused operator: each factory is a
|
|
23
25
|
* standalone function and the union-type definition compiles away.
|
|
24
26
|
*/
|
|
25
|
-
export type OperatorKind = "where" | "select" | "selectMany" | "take" | "skip" | "takeWhile" | "skipWhile" | "distinct" | "distinctBy" | "concat" | "union" | "intersect" | "except" | "append" | "prepend" | "reverse" | "orderBy" | "orderByDescending" | "thenBy" | "thenByDescending" | "groupBy" | "zip";
|
|
26
|
-
export type TerminalKind = "toArray" | "toMap" | "toSet" | "toList" | "toDictionary" | "toHashSet" | "first" | "firstOrDefault" | "last" | "lastOrDefault" | "single" | "singleOrDefault" | "any" | "all" | "count" | "sum" | "min" | "max" | "minBy" | "maxBy" | "average" | "contains" | "aggregate";
|
|
27
|
+
export type OperatorKind = "where" | "select" | "selectMany" | "take" | "skip" | "takeWhile" | "skipWhile" | "distinct" | "distinctBy" | "concat" | "union" | "intersect" | "except" | "append" | "prepend" | "reverse" | "orderBy" | "orderByDescending" | "thenBy" | "thenByDescending" | "groupBy" | "join" | "zip" | "defaultIfEmpty";
|
|
28
|
+
export type TerminalKind = "toArray" | "toMap" | "toSet" | "toList" | "toDictionary" | "toHashSet" | "toLookup" | "sequenceEqual" | "first" | "firstOrDefault" | "last" | "lastOrDefault" | "single" | "singleOrDefault" | "any" | "all" | "count" | "sum" | "min" | "max" | "minBy" | "maxBy" | "average" | "contains" | "aggregate" | "elementAt" | "elementAtOrDefault";
|
|
27
29
|
/**
|
|
28
30
|
* Common shape every composition operator shares: a discriminator (`kind`)
|
|
29
31
|
* + a runtime walker (`apply`). Concrete descriptors extend this with the
|
|
@@ -75,6 +77,7 @@ export interface SkipWhileOp<T> extends OperatorBase<T, T> {
|
|
|
75
77
|
}
|
|
76
78
|
export interface DistinctOp<T> extends OperatorBase<T, T> {
|
|
77
79
|
readonly kind: "distinct";
|
|
80
|
+
readonly comparer?: IEqualityComparer<T> | null;
|
|
78
81
|
}
|
|
79
82
|
export interface DistinctByOp<T, K> extends OperatorBase<T, T> {
|
|
80
83
|
readonly kind: "distinctBy";
|
|
@@ -137,6 +140,14 @@ export interface ZipOp<T, U, R> extends OperatorBase<T, R> {
|
|
|
137
140
|
readonly resultSelector: (first: T, second: U) => R;
|
|
138
141
|
readonly queryable?: QueryableMeta;
|
|
139
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* `DefaultIfEmpty`: the source, or `defaultValue` alone when the source is empty. `D` is the
|
|
145
|
+
* miss value: `null` for a reference or nullable element, the element type otherwise.
|
|
146
|
+
*/
|
|
147
|
+
export interface DefaultIfEmptyOp<T, D extends T | null = T | null> extends OperatorBase<T, T | D> {
|
|
148
|
+
readonly kind: "defaultIfEmpty";
|
|
149
|
+
readonly defaultValue: D;
|
|
150
|
+
}
|
|
140
151
|
/**
|
|
141
152
|
* Group elements by a key. Each yielded item is an
|
|
142
153
|
* <see cref="Grouping{K, T}"/> — an Iterable that also exposes the
|
|
@@ -149,10 +160,20 @@ export interface Grouping<K, T> extends Iterable<T> {
|
|
|
149
160
|
export interface GroupByOp<T, K> extends OperatorBase<T, Grouping<K, T>> {
|
|
150
161
|
readonly kind: "groupBy";
|
|
151
162
|
readonly keySelector: (item: T) => K;
|
|
163
|
+
readonly comparer?: IEqualityComparer<K> | null;
|
|
152
164
|
readonly queryable?: QueryableMeta;
|
|
153
165
|
}
|
|
166
|
+
/** `Join(inner, outerKeySelector, innerKeySelector, resultSelector[, comparer])`. */
|
|
167
|
+
export interface JoinOp<T, U, K, R> extends OperatorBase<T, R> {
|
|
168
|
+
readonly kind: "join";
|
|
169
|
+
readonly inner: Iterable<U>;
|
|
170
|
+
readonly outerKeySelector: (item: T) => K;
|
|
171
|
+
readonly innerKeySelector: (item: U) => K;
|
|
172
|
+
readonly resultSelector: (outer: T, inner: U) => R;
|
|
173
|
+
readonly comparer?: IEqualityComparer<K> | null;
|
|
174
|
+
}
|
|
154
175
|
/** Discriminated union of every composition operator. */
|
|
155
|
-
export type AnyOperator = WhereOp<unknown> | SelectOp<unknown, unknown> | SelectManyOp<unknown, unknown> | TakeOp<unknown> | SkipOp<unknown> | TakeWhileOp<unknown> | SkipWhileOp<unknown> | DistinctOp<unknown> | DistinctByOp<unknown, unknown> | ConcatOp<unknown> | UnionOp<unknown> | IntersectOp<unknown> | ExceptOp<unknown> | AppendOp<unknown> | PrependOp<unknown> | ReverseOp<unknown> | OrderByOp<unknown, unknown> | OrderByDescendingOp<unknown, unknown> | ThenByOp<unknown, unknown> | ThenByDescendingOp<unknown, unknown> | GroupByOp<unknown, unknown> | ZipOp<unknown, unknown, unknown>;
|
|
176
|
+
export type AnyOperator = WhereOp<unknown> | SelectOp<unknown, unknown> | SelectManyOp<unknown, unknown> | TakeOp<unknown> | SkipOp<unknown> | TakeWhileOp<unknown> | SkipWhileOp<unknown> | DistinctOp<unknown> | DistinctByOp<unknown, unknown> | ConcatOp<unknown> | UnionOp<unknown> | IntersectOp<unknown> | ExceptOp<unknown> | AppendOp<unknown> | PrependOp<unknown> | ReverseOp<unknown> | OrderByOp<unknown, unknown> | OrderByDescendingOp<unknown, unknown> | ThenByOp<unknown, unknown> | ThenByDescendingOp<unknown, unknown> | GroupByOp<unknown, unknown> | JoinOp<unknown, unknown, unknown, unknown> | ZipOp<unknown, unknown, unknown> | DefaultIfEmptyOp<unknown>;
|
|
156
177
|
/**
|
|
157
178
|
* Common shape every terminal shares. `apply` walks the source and
|
|
158
179
|
* produces a concrete value; providers read `kind` + the captured
|
|
@@ -203,6 +224,17 @@ export interface FirstOrDefaultTerm<T, D extends T | null = T | null> extends Te
|
|
|
203
224
|
readonly defaultValue: D;
|
|
204
225
|
readonly queryable?: QueryableMeta;
|
|
205
226
|
}
|
|
227
|
+
/** `ElementAt(index)`: throws `ArgumentOutOfRangeException` when `index` is outside the source. */
|
|
228
|
+
export interface ElementAtTerm<T> extends TerminalBase<T, T> {
|
|
229
|
+
readonly kind: "elementAt";
|
|
230
|
+
readonly index: number;
|
|
231
|
+
}
|
|
232
|
+
/** `ElementAtOrDefault(index)`: `defaultValue` when `index` is outside the source. */
|
|
233
|
+
export interface ElementAtOrDefaultTerm<T, D extends T | null = T | null> extends TerminalBase<T, T | D> {
|
|
234
|
+
readonly kind: "elementAtOrDefault";
|
|
235
|
+
readonly index: number;
|
|
236
|
+
readonly defaultValue: D;
|
|
237
|
+
}
|
|
206
238
|
export interface LastTerm<T> extends TerminalBase<T, T> {
|
|
207
239
|
readonly kind: "last";
|
|
208
240
|
readonly predicate?: (item: T) => boolean;
|
|
@@ -320,6 +352,18 @@ export interface MaxInt64Term<T, R extends bigint | null> extends TerminalBase<T
|
|
|
320
352
|
export interface ContainsTerm<T> extends TerminalBase<T, boolean> {
|
|
321
353
|
readonly kind: "contains";
|
|
322
354
|
readonly value: T;
|
|
355
|
+
readonly comparer?: IEqualityComparer<T> | null;
|
|
356
|
+
}
|
|
357
|
+
export interface SequenceEqualTerm<T> extends TerminalBase<T, boolean> {
|
|
358
|
+
readonly kind: "sequenceEqual";
|
|
359
|
+
readonly second: Iterable<T>;
|
|
360
|
+
readonly comparer?: IEqualityComparer<T> | null;
|
|
361
|
+
}
|
|
362
|
+
export interface ToLookupTerm<T, K, V> extends TerminalBase<T, Lookup<K, V>> {
|
|
363
|
+
readonly kind: "toLookup";
|
|
364
|
+
readonly keySelector: (item: T) => K;
|
|
365
|
+
readonly elementSelector?: (item: T) => V;
|
|
366
|
+
readonly comparer?: IEqualityComparer<K> | null;
|
|
323
367
|
}
|
|
324
368
|
export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
|
|
325
369
|
readonly kind: "aggregate";
|
|
@@ -327,4 +371,4 @@ export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
|
|
|
327
371
|
readonly accumulator: (acc: A, item: T) => A;
|
|
328
372
|
}
|
|
329
373
|
/** Discriminated union of every terminal. */
|
|
330
|
-
export type AnyTerminal = ToArrayTerm<unknown> | ToMapTerm<unknown, unknown, unknown> | ToSetTerm<unknown> | ToListTerm<unknown> | ToDictionaryTerm<unknown, unknown, unknown> | ToHashSetTerm<unknown> | FirstTerm<unknown> | FirstOrDefaultTerm<unknown> | LastTerm<unknown> | LastOrDefaultTerm<unknown> | SingleTerm<unknown> | SingleOrDefaultTerm<unknown> | AnyTerm<unknown> | AllTerm<unknown> | CountTerm<unknown> | SumTerm<unknown, unknown> | MinTerm<unknown, unknown> | MaxTerm<unknown, unknown> | MinByTerm<unknown, unknown, unknown> | MaxByTerm<unknown, unknown, unknown> | AverageTerm<unknown, unknown> | SumInt64Term<unknown> | AverageInt64Term<unknown, number | null> | MinInt64Term<unknown, bigint | null> | MaxInt64Term<unknown, bigint | null> | ContainsTerm<unknown> | AggregateTerm<unknown, unknown>;
|
|
374
|
+
export type AnyTerminal = ToArrayTerm<unknown> | ToMapTerm<unknown, unknown, unknown> | ToSetTerm<unknown> | ToListTerm<unknown> | ToDictionaryTerm<unknown, unknown, unknown> | ToHashSetTerm<unknown> | FirstTerm<unknown> | FirstOrDefaultTerm<unknown> | ElementAtTerm<unknown> | ElementAtOrDefaultTerm<unknown> | LastTerm<unknown> | LastOrDefaultTerm<unknown> | SingleTerm<unknown> | SingleOrDefaultTerm<unknown> | AnyTerm<unknown> | AllTerm<unknown> | CountTerm<unknown> | SumTerm<unknown, unknown> | MinTerm<unknown, unknown> | MaxTerm<unknown, unknown> | MinByTerm<unknown, unknown, unknown> | MaxByTerm<unknown, unknown, unknown> | AverageTerm<unknown, unknown> | SumInt64Term<unknown> | AverageInt64Term<unknown, number | null> | MinInt64Term<unknown, bigint | null> | MaxInt64Term<unknown, bigint | null> | ContainsTerm<unknown> | SequenceEqualTerm<unknown> | ToLookupTerm<unknown, unknown, unknown> | AggregateTerm<unknown, unknown>;
|
package/dist/system/math.d.ts
CHANGED
|
@@ -53,3 +53,10 @@ export declare function roundNumber(value: number, digits?: number, mode?: Midpo
|
|
|
53
53
|
* .NET's messages.
|
|
54
54
|
*/
|
|
55
55
|
export declare function roundDecimal<D extends DecimalRoundable<D>>(value: D, digits?: number, mode?: MidpointRounding): D;
|
|
56
|
+
/**
|
|
57
|
+
* `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
|
|
58
|
+
* `int.MinValue`, whose magnitude does not fit `int`.
|
|
59
|
+
*
|
|
60
|
+
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
61
|
+
*/
|
|
62
|
+
export declare function absInt32(value: number): number;
|
package/dist/system/math.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
12
12
|
import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.js";
|
|
13
|
+
import { OverflowException } from "./exceptions/overflow-exception.js";
|
|
13
14
|
/**
|
|
14
15
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
15
16
|
* value read from serialized C# data keeps its meaning.
|
|
@@ -139,3 +140,16 @@ function assertMode(mode) {
|
|
|
139
140
|
throw new ArgumentException(`The value '${mode}' is not valid for this usage of the type MidpointRounding.`, "mode");
|
|
140
141
|
}
|
|
141
142
|
}
|
|
143
|
+
const INT32_MIN = -2147483648;
|
|
144
|
+
/**
|
|
145
|
+
* `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
|
|
146
|
+
* `int.MinValue`, whose magnitude does not fit `int`.
|
|
147
|
+
*
|
|
148
|
+
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
149
|
+
*/
|
|
150
|
+
export function absInt32(value) {
|
|
151
|
+
if (value === INT32_MIN) {
|
|
152
|
+
throw new OverflowException("Negating the minimum value of a twos complement number is invalid.");
|
|
153
|
+
}
|
|
154
|
+
return Math.abs(value);
|
|
155
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `int.CompareTo(int)`, `uint.CompareTo(uint)`, `double.CompareTo(double)`: `-1`, `0` or `1`.
|
|
3
|
+
* For a double, `NaN` is less than every other number and equal to itself, and `-0` equals `0`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function compareNumber(value: number, other: number): number;
|
|
6
|
+
/**
|
|
7
|
+
* `Convert.ToDouble(string)`: `null` converts to `0`, any other text parses as
|
|
8
|
+
* `double.Parse` does.
|
|
9
|
+
*/
|
|
10
|
+
export declare function convertStringToDouble(text: string | null | undefined): number;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Members of the .NET numeric types that JavaScript numbers do not have
|
|
3
|
+
* (`int.CompareTo`, `double.CompareTo`), with .NET's results.
|
|
4
|
+
*/
|
|
5
|
+
import { parseFloat64 } from "./number-parsing.js";
|
|
6
|
+
/**
|
|
7
|
+
* `int.CompareTo(int)`, `uint.CompareTo(uint)`, `double.CompareTo(double)`: `-1`, `0` or `1`.
|
|
8
|
+
* For a double, `NaN` is less than every other number and equal to itself, and `-0` equals `0`.
|
|
9
|
+
*/
|
|
10
|
+
export function compareNumber(value, other) {
|
|
11
|
+
if (Number.isNaN(value)) {
|
|
12
|
+
return Number.isNaN(other) ? 0 : -1;
|
|
13
|
+
}
|
|
14
|
+
if (Number.isNaN(other)) {
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
return value < other ? -1 : value > other ? 1 : 0;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `Convert.ToDouble(string)`: `null` converts to `0`, any other text parses as
|
|
21
|
+
* `double.Parse` does.
|
|
22
|
+
*/
|
|
23
|
+
export function convertStringToDouble(text) {
|
|
24
|
+
return text == null ? 0 : parseFloat64(text);
|
|
25
|
+
}
|
|
@@ -31,24 +31,36 @@ export declare function tryParseByte(text: string | null | undefined, style?: nu
|
|
|
31
31
|
/** Mirrors `sbyte.TryParse(string, out sbyte)` and its `NumberStyles` overloads. */
|
|
32
32
|
export declare function tryParseSByte(text: string | null | undefined, style?: number): ParseResult<number>;
|
|
33
33
|
/**
|
|
34
|
-
* Mirrors `double.TryParse(string, out double)
|
|
35
|
-
* generated code holds as `number` too). A value beyond the `double` range parses to
|
|
34
|
+
* Mirrors `double.TryParse(string, out double)`. A value beyond the `double` range parses to
|
|
36
35
|
* an infinity, as it does since .NET Core 3.0.
|
|
37
36
|
*/
|
|
38
37
|
export declare function tryParseFloat64(text: string | null | undefined): ParseResult<number>;
|
|
38
|
+
/**
|
|
39
|
+
* Mirrors `float.TryParse(string, out float)`: {@link tryParseFloat64}'s grammar, the value
|
|
40
|
+
* rounded to single precision as .NET's parser rounds it (#695). A value beyond the
|
|
41
|
+
* `float` range parses to an infinity.
|
|
42
|
+
*/
|
|
43
|
+
export declare function tryParseFloat32(text: string | null | undefined): ParseResult<number>;
|
|
39
44
|
/**
|
|
40
45
|
* Mirrors `bool.TryParse(string, out bool)`: `true` / `false` in any letter case, around
|
|
41
46
|
* whitespace (JavaScript's `\s`, close to .NET's `char.IsWhiteSpace`) and `\0` characters.
|
|
42
47
|
*/
|
|
43
48
|
export declare function tryParseBoolean(text: string | null | undefined): ParseResult<boolean>;
|
|
44
49
|
/**
|
|
45
|
-
* Mirrors `double.Parse(string)
|
|
46
|
-
* `number` too).
|
|
50
|
+
* Mirrors `double.Parse(string)`.
|
|
47
51
|
*
|
|
48
52
|
* @throws ArgumentNullException for `null`.
|
|
49
53
|
* @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
|
|
50
54
|
*/
|
|
51
55
|
export declare function parseFloat64(text: string | null | undefined): number;
|
|
56
|
+
/**
|
|
57
|
+
* Mirrors `float.Parse(string)`: {@link parseFloat64}, the value rounded to single precision
|
|
58
|
+
* as .NET's parser rounds it (#695).
|
|
59
|
+
*
|
|
60
|
+
* @throws ArgumentNullException for `null`.
|
|
61
|
+
* @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
|
|
62
|
+
*/
|
|
63
|
+
export declare function parseFloat32(text: string | null | undefined): number;
|
|
52
64
|
/**
|
|
53
65
|
* Mirrors `bool.Parse(string)`.
|
|
54
66
|
*
|
|
@@ -105,8 +105,7 @@ function numberResult(result) {
|
|
|
105
105
|
return [result[0], Number(result[1])];
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
|
-
* Mirrors `double.TryParse(string, out double)
|
|
109
|
-
* generated code holds as `number` too). A value beyond the `double` range parses to
|
|
108
|
+
* Mirrors `double.TryParse(string, out double)`. A value beyond the `double` range parses to
|
|
110
109
|
* an infinity, as it does since .NET Core 3.0.
|
|
111
110
|
*/
|
|
112
111
|
export function tryParseFloat64(text) {
|
|
@@ -120,6 +119,15 @@ export function tryParseFloat64(text) {
|
|
|
120
119
|
}
|
|
121
120
|
return FLOAT.test(trimmed) ? [true, Number(trimmed.replace(/,/g, ""))] : [false, 0];
|
|
122
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Mirrors `float.TryParse(string, out float)`: {@link tryParseFloat64}'s grammar, the value
|
|
124
|
+
* rounded to single precision as .NET's parser rounds it (#695). A value beyond the
|
|
125
|
+
* `float` range parses to an infinity.
|
|
126
|
+
*/
|
|
127
|
+
export function tryParseFloat32(text) {
|
|
128
|
+
const [found, value] = tryParseFloat64(text);
|
|
129
|
+
return [found, Math.fround(value)];
|
|
130
|
+
}
|
|
123
131
|
/**
|
|
124
132
|
* Mirrors `bool.TryParse(string, out bool)`: `true` / `false` in any letter case, around
|
|
125
133
|
* whitespace (JavaScript's `\s`, close to .NET's `char.IsWhiteSpace`) and `\0` characters.
|
|
@@ -132,8 +140,7 @@ export function tryParseBoolean(text) {
|
|
|
132
140
|
return [word === "false", false];
|
|
133
141
|
}
|
|
134
142
|
/**
|
|
135
|
-
* Mirrors `double.Parse(string)
|
|
136
|
-
* `number` too).
|
|
143
|
+
* Mirrors `double.Parse(string)`.
|
|
137
144
|
*
|
|
138
145
|
* @throws ArgumentNullException for `null`.
|
|
139
146
|
* @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
|
|
@@ -148,6 +155,16 @@ export function parseFloat64(text) {
|
|
|
148
155
|
}
|
|
149
156
|
return value;
|
|
150
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Mirrors `float.Parse(string)`: {@link parseFloat64}, the value rounded to single precision
|
|
160
|
+
* as .NET's parser rounds it (#695).
|
|
161
|
+
*
|
|
162
|
+
* @throws ArgumentNullException for `null`.
|
|
163
|
+
* @throws FormatException for text outside the grammar of {@link tryParseFloat64}.
|
|
164
|
+
*/
|
|
165
|
+
export function parseFloat32(text) {
|
|
166
|
+
return Math.fround(parseFloat64(text));
|
|
167
|
+
}
|
|
151
168
|
/**
|
|
152
169
|
* Mirrors `bool.Parse(string)`.
|
|
153
170
|
*
|