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
package/src/system/linq/types.ts
CHANGED
|
@@ -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
|
/**
|
|
7
9
|
* Descriptor-based LINQ runtime — prototype for #20.
|
|
@@ -46,7 +48,9 @@ export type OperatorKind =
|
|
|
46
48
|
| "thenBy"
|
|
47
49
|
| "thenByDescending"
|
|
48
50
|
| "groupBy"
|
|
49
|
-
| "
|
|
51
|
+
| "join"
|
|
52
|
+
| "zip"
|
|
53
|
+
| "defaultIfEmpty";
|
|
50
54
|
|
|
51
55
|
export type TerminalKind =
|
|
52
56
|
| "toArray"
|
|
@@ -55,6 +59,8 @@ export type TerminalKind =
|
|
|
55
59
|
| "toList"
|
|
56
60
|
| "toDictionary"
|
|
57
61
|
| "toHashSet"
|
|
62
|
+
| "toLookup"
|
|
63
|
+
| "sequenceEqual"
|
|
58
64
|
| "first"
|
|
59
65
|
| "firstOrDefault"
|
|
60
66
|
| "last"
|
|
@@ -71,7 +77,9 @@ export type TerminalKind =
|
|
|
71
77
|
| "maxBy"
|
|
72
78
|
| "average"
|
|
73
79
|
| "contains"
|
|
74
|
-
| "aggregate"
|
|
80
|
+
| "aggregate"
|
|
81
|
+
| "elementAt"
|
|
82
|
+
| "elementAtOrDefault";
|
|
75
83
|
|
|
76
84
|
/**
|
|
77
85
|
* Common shape every composition operator shares: a discriminator (`kind`)
|
|
@@ -132,6 +140,7 @@ export interface SkipWhileOp<T> extends OperatorBase<T, T> {
|
|
|
132
140
|
|
|
133
141
|
export interface DistinctOp<T> extends OperatorBase<T, T> {
|
|
134
142
|
readonly kind: "distinct";
|
|
143
|
+
readonly comparer?: IEqualityComparer<T> | null;
|
|
135
144
|
}
|
|
136
145
|
|
|
137
146
|
export interface DistinctByOp<T, K> extends OperatorBase<T, T> {
|
|
@@ -208,6 +217,15 @@ export interface ZipOp<T, U, R> extends OperatorBase<T, R> {
|
|
|
208
217
|
readonly queryable?: QueryableMeta;
|
|
209
218
|
}
|
|
210
219
|
|
|
220
|
+
/**
|
|
221
|
+
* `DefaultIfEmpty`: the source, or `defaultValue` alone when the source is empty. `D` is the
|
|
222
|
+
* miss value: `null` for a reference or nullable element, the element type otherwise.
|
|
223
|
+
*/
|
|
224
|
+
export interface DefaultIfEmptyOp<T, D extends T | null = T | null> extends OperatorBase<T, T | D> {
|
|
225
|
+
readonly kind: "defaultIfEmpty";
|
|
226
|
+
readonly defaultValue: D;
|
|
227
|
+
}
|
|
228
|
+
|
|
211
229
|
/**
|
|
212
230
|
* Group elements by a key. Each yielded item is an
|
|
213
231
|
* <see cref="Grouping{K, T}"/> — an Iterable that also exposes the
|
|
@@ -221,9 +239,20 @@ export interface Grouping<K, T> extends Iterable<T> {
|
|
|
221
239
|
export interface GroupByOp<T, K> extends OperatorBase<T, Grouping<K, T>> {
|
|
222
240
|
readonly kind: "groupBy";
|
|
223
241
|
readonly keySelector: (item: T) => K;
|
|
242
|
+
readonly comparer?: IEqualityComparer<K> | null;
|
|
224
243
|
readonly queryable?: QueryableMeta;
|
|
225
244
|
}
|
|
226
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
|
+
|
|
227
256
|
/** Discriminated union of every composition operator. */
|
|
228
257
|
export type AnyOperator =
|
|
229
258
|
| WhereOp<unknown>
|
|
@@ -247,7 +276,9 @@ export type AnyOperator =
|
|
|
247
276
|
| ThenByOp<unknown, unknown>
|
|
248
277
|
| ThenByDescendingOp<unknown, unknown>
|
|
249
278
|
| GroupByOp<unknown, unknown>
|
|
250
|
-
|
|
|
279
|
+
| JoinOp<unknown, unknown, unknown, unknown>
|
|
280
|
+
| ZipOp<unknown, unknown, unknown>
|
|
281
|
+
| DefaultIfEmptyOp<unknown>;
|
|
251
282
|
|
|
252
283
|
/**
|
|
253
284
|
* Common shape every terminal shares. `apply` walks the source and
|
|
@@ -309,6 +340,20 @@ export interface FirstOrDefaultTerm<T, D extends T | null = T | null>
|
|
|
309
340
|
readonly queryable?: QueryableMeta;
|
|
310
341
|
}
|
|
311
342
|
|
|
343
|
+
/** `ElementAt(index)`: throws `ArgumentOutOfRangeException` when `index` is outside the source. */
|
|
344
|
+
export interface ElementAtTerm<T> extends TerminalBase<T, T> {
|
|
345
|
+
readonly kind: "elementAt";
|
|
346
|
+
readonly index: number;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** `ElementAtOrDefault(index)`: `defaultValue` when `index` is outside the source. */
|
|
350
|
+
export interface ElementAtOrDefaultTerm<T, D extends T | null = T | null>
|
|
351
|
+
extends TerminalBase<T, T | D> {
|
|
352
|
+
readonly kind: "elementAtOrDefault";
|
|
353
|
+
readonly index: number;
|
|
354
|
+
readonly defaultValue: D;
|
|
355
|
+
}
|
|
356
|
+
|
|
312
357
|
export interface LastTerm<T> extends TerminalBase<T, T> {
|
|
313
358
|
readonly kind: "last";
|
|
314
359
|
readonly predicate?: (item: T) => boolean;
|
|
@@ -445,6 +490,20 @@ export interface MaxInt64Term<T, R extends bigint | null> extends TerminalBase<T
|
|
|
445
490
|
export interface ContainsTerm<T> extends TerminalBase<T, boolean> {
|
|
446
491
|
readonly kind: "contains";
|
|
447
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;
|
|
448
507
|
}
|
|
449
508
|
|
|
450
509
|
export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
|
|
@@ -463,6 +522,8 @@ export type AnyTerminal =
|
|
|
463
522
|
| ToHashSetTerm<unknown>
|
|
464
523
|
| FirstTerm<unknown>
|
|
465
524
|
| FirstOrDefaultTerm<unknown>
|
|
525
|
+
| ElementAtTerm<unknown>
|
|
526
|
+
| ElementAtOrDefaultTerm<unknown>
|
|
466
527
|
| LastTerm<unknown>
|
|
467
528
|
| LastOrDefaultTerm<unknown>
|
|
468
529
|
| SingleTerm<unknown>
|
|
@@ -481,4 +542,6 @@ export type AnyTerminal =
|
|
|
481
542
|
| MinInt64Term<unknown, bigint | null>
|
|
482
543
|
| MaxInt64Term<unknown, bigint | null>
|
|
483
544
|
| ContainsTerm<unknown>
|
|
545
|
+
| SequenceEqualTerm<unknown>
|
|
546
|
+
| ToLookupTerm<unknown, unknown, unknown>
|
|
484
547
|
| AggregateTerm<unknown, unknown>;
|
package/src/system/math.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { ArgumentException } from "./exceptions/argument-exception.ts";
|
|
13
13
|
import { ArgumentOutOfRangeException } from "./exceptions/argument-out-of-range-exception.ts";
|
|
14
|
+
import { OverflowException } from "./exceptions/overflow-exception.ts";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
@@ -180,3 +181,21 @@ function assertMode(mode: number): asserts mode is MidpointRounding {
|
|
|
180
181
|
);
|
|
181
182
|
}
|
|
182
183
|
}
|
|
184
|
+
|
|
185
|
+
const INT32_MIN = -2147483648;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
|
|
189
|
+
* `int.MinValue`, whose magnitude does not fit `int`.
|
|
190
|
+
*
|
|
191
|
+
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
192
|
+
*/
|
|
193
|
+
export function absInt32(value: number): number {
|
|
194
|
+
if (value === INT32_MIN) {
|
|
195
|
+
throw new OverflowException(
|
|
196
|
+
"Negating the minimum value of a twos complement number is invalid.",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return Math.abs(value);
|
|
201
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `int.CompareTo(int)`, `uint.CompareTo(uint)`, `double.CompareTo(double)`: `-1`, `0` or `1`.
|
|
9
|
+
* For a double, `NaN` is less than every other number and equal to itself, and `-0` equals `0`.
|
|
10
|
+
*/
|
|
11
|
+
export function compareNumber(value: number, other: number): number {
|
|
12
|
+
if (Number.isNaN(value)) {
|
|
13
|
+
return Number.isNaN(other) ? 0 : -1;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (Number.isNaN(other)) {
|
|
17
|
+
return 1;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return value < other ? -1 : value > other ? 1 : 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `Convert.ToDouble(string)`: `null` converts to `0`, any other text parses as
|
|
25
|
+
* `double.Parse` does.
|
|
26
|
+
*/
|
|
27
|
+
export function convertStringToDouble(text: string | null | undefined): number {
|
|
28
|
+
return text == null ? 0 : parseFloat64(text);
|
|
29
|
+
}
|
|
@@ -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
|
*
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ArgumentOutOfRangeException } from "./exceptions/index.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `System.Text.StringBuilder`: a mutable text. Every mutator returns the builder, so calls
|
|
5
|
+
* chain as they do in C#. `AppendLine` adds `\n` (`Environment.NewLine` on Unix); an index
|
|
6
|
+
* or count outside the text throws `ArgumentOutOfRangeException`.
|
|
7
|
+
*/
|
|
8
|
+
export class StringBuilder {
|
|
9
|
+
private text: string;
|
|
10
|
+
|
|
11
|
+
constructor(initial?: string | null) {
|
|
12
|
+
this.text = initial ?? "";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The number of characters; assigning truncates, or pads with NUL characters. */
|
|
16
|
+
get length(): number {
|
|
17
|
+
return this.text.length;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
set length(value: number) {
|
|
21
|
+
if (value < 0) {
|
|
22
|
+
throw new ArgumentOutOfRangeException("value", value, "Length cannot be less than zero.");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.text =
|
|
26
|
+
value <= this.text.length
|
|
27
|
+
? this.text.slice(0, value)
|
|
28
|
+
: this.text + "\0".repeat(value - this.text.length);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** `Append(value)`: `null` appends nothing. */
|
|
32
|
+
append(value: string | null | undefined): this {
|
|
33
|
+
this.text += value ?? "";
|
|
34
|
+
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `Append(char, repeatCount)`. */
|
|
39
|
+
appendRepeated(value: string, repeatCount: number): this {
|
|
40
|
+
if (repeatCount < 0) {
|
|
41
|
+
throw new ArgumentOutOfRangeException(
|
|
42
|
+
"repeatCount",
|
|
43
|
+
repeatCount,
|
|
44
|
+
`repeatCount ('${repeatCount}') must be a non-negative value.`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.text += value.repeat(repeatCount);
|
|
49
|
+
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `AppendLine(value)`. */
|
|
54
|
+
appendLine(value?: string | null): this {
|
|
55
|
+
this.text += `${value ?? ""}\n`;
|
|
56
|
+
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** `Clear()`. */
|
|
61
|
+
clear(): this {
|
|
62
|
+
this.text = "";
|
|
63
|
+
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** `Insert(index, value)`. */
|
|
68
|
+
insert(index: number, value: string | null | undefined): this {
|
|
69
|
+
requireInRange(index, 0, this.text.length, "index");
|
|
70
|
+
this.text = this.text.slice(0, index) + (value ?? "") + this.text.slice(index);
|
|
71
|
+
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** `Remove(startIndex, length)`. */
|
|
76
|
+
remove(startIndex: number, length: number): this {
|
|
77
|
+
requireInRange(length, 0, this.text.length, "length");
|
|
78
|
+
requireInRange(startIndex, 0, this.text.length - length, "startIndex");
|
|
79
|
+
this.text = this.text.slice(0, startIndex) + this.text.slice(startIndex + length);
|
|
80
|
+
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `Replace(oldValue, newValue)`: every occurrence, literally. */
|
|
85
|
+
replace(oldValue: string, newValue: string | null | undefined): this {
|
|
86
|
+
if (oldValue === "") {
|
|
87
|
+
throw new ArgumentOutOfRangeException("oldValue", oldValue, "The value cannot be empty.");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.text = this.text.split(oldValue).join(newValue ?? "");
|
|
91
|
+
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** `ToString()`. */
|
|
96
|
+
toString(): string {
|
|
97
|
+
return this.text;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `ToString(startIndex, length)`. */
|
|
101
|
+
toSubstring(startIndex: number, length: number): string {
|
|
102
|
+
requireInRange(length, 0, this.text.length, "length");
|
|
103
|
+
requireInRange(startIndex, 0, this.text.length - length, "startIndex");
|
|
104
|
+
|
|
105
|
+
return this.text.slice(startIndex, startIndex + length);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const INDEX_AT_MOST_COUNT_MESSAGE =
|
|
110
|
+
"Index was out of range. Must be non-negative and less than or equal to the size of the collection.";
|
|
111
|
+
|
|
112
|
+
/** An index or length outside the text. */
|
|
113
|
+
function requireInRange(value: number, min: number, max: number, parameter: string): void {
|
|
114
|
+
if (value < min || value > max) {
|
|
115
|
+
throw new ArgumentOutOfRangeException(parameter, undefined, INDEX_AT_MOST_COUNT_MESSAGE);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ArgumentException,
|
|
3
|
+
ArgumentNullException,
|
|
4
|
+
InvalidOperationException,
|
|
5
|
+
TaskCanceledException,
|
|
6
|
+
} from "./exceptions/index.ts";
|
|
7
|
+
|
|
8
|
+
const ALREADY_COMPLETED_MESSAGE =
|
|
9
|
+
"An attempt was made to transition a task to a final state when it had already completed.";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `System.Threading.Tasks.TaskCompletionSource<T>`: a promise (`task`) settled from outside.
|
|
13
|
+
* The first `set*` / `trySet*` call wins; `set*` after that throws `InvalidOperationException`
|
|
14
|
+
* and `trySet*` returns `false`. A faulted or canceled task whose rejection nobody observes
|
|
15
|
+
* does not become an unhandled rejection, as an unobserved .NET task does not crash.
|
|
16
|
+
*/
|
|
17
|
+
export class TaskCompletionSource<T> {
|
|
18
|
+
readonly task: Promise<T>;
|
|
19
|
+
private resolveTask!: (value: T) => void;
|
|
20
|
+
private rejectTask!: (error: unknown) => void;
|
|
21
|
+
private completed = false;
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
this.task = new Promise<T>((resolve, reject) => {
|
|
25
|
+
this.resolveTask = resolve;
|
|
26
|
+
this.rejectTask = reject;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
this.task.catch(() => undefined);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** `TrySetResult(value)`. */
|
|
33
|
+
trySetResult(value: T): boolean {
|
|
34
|
+
return this.complete(() => this.resolveTask(value));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `TrySetException(exception)`. */
|
|
38
|
+
trySetException(exception: Error): boolean {
|
|
39
|
+
return this.complete(() => this.rejectTask(exception));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `TrySetException(IEnumerable<Exception>)`: the task faults with the first exception, which
|
|
44
|
+
* is what awaiting it throws.
|
|
45
|
+
*
|
|
46
|
+
* @throws ArgumentNullException for `null`.
|
|
47
|
+
* @throws ArgumentException for an empty sequence.
|
|
48
|
+
*/
|
|
49
|
+
trySetExceptions(exceptions: Iterable<Error> | null): boolean {
|
|
50
|
+
return this.trySetException(firstException(exceptions));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `SetException(IEnumerable<Exception>)`. */
|
|
54
|
+
setExceptions(exceptions: Iterable<Error> | null): void {
|
|
55
|
+
this.setException(firstException(exceptions));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `TrySetCanceled()`. */
|
|
59
|
+
trySetCanceled(): boolean {
|
|
60
|
+
return this.complete(() => this.rejectTask(new TaskCanceledException()));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `SetResult(value)`. */
|
|
64
|
+
setResult(value: T): void {
|
|
65
|
+
this.require(this.trySetResult(value));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `SetException(exception)`. */
|
|
69
|
+
setException(exception: Error): void {
|
|
70
|
+
this.require(this.trySetException(exception));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** `SetCanceled()`. */
|
|
74
|
+
setCanceled(): void {
|
|
75
|
+
this.require(this.trySetCanceled());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private complete(settle: () => void): boolean {
|
|
79
|
+
if (this.completed) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.completed = true;
|
|
84
|
+
settle();
|
|
85
|
+
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private require(succeeded: boolean): void {
|
|
90
|
+
if (!succeeded) {
|
|
91
|
+
throw new InvalidOperationException(ALREADY_COMPLETED_MESSAGE);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function firstException(exceptions: Iterable<Error> | null): Error {
|
|
97
|
+
if (exceptions === null) {
|
|
98
|
+
throw new ArgumentNullException("exceptions");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (const exception of exceptions) {
|
|
102
|
+
return exception;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
throw new ArgumentException("The exceptions collection was empty.", "exceptions");
|
|
106
|
+
}
|