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
|
@@ -9,13 +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";
|
|
14
|
-
import { throwOOR } from "../collections/throw-helpers.js";
|
|
15
|
+
import { throwArgumentNull, throwOOR } from "../collections/throw-helpers.js";
|
|
15
16
|
import { List } from "../collections/list.js";
|
|
16
17
|
import { ArgumentOutOfRangeException } from "../exceptions/argument-out-of-range-exception.js";
|
|
17
18
|
import { InvalidOperationException } from "../exceptions/invalid-operation-exception.js";
|
|
18
19
|
import { compareKeys } from "./compare-keys.js";
|
|
20
|
+
import { bucketsOf } from "./grouping.js";
|
|
21
|
+
import { Lookup } from "./lookup.js";
|
|
19
22
|
export function toArray() {
|
|
20
23
|
return {
|
|
21
24
|
kind: "toArray",
|
|
@@ -399,18 +402,83 @@ export function averageNullable(selector, queryable) {
|
|
|
399
402
|
apply: (source) => averageOf(valuesOf(source, selector)),
|
|
400
403
|
};
|
|
401
404
|
}
|
|
402
|
-
|
|
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) {
|
|
403
410
|
return {
|
|
404
411
|
kind: "contains",
|
|
405
412
|
value,
|
|
413
|
+
comparer,
|
|
406
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
|
+
}
|
|
407
423
|
for (const element of source)
|
|
408
|
-
if (element
|
|
424
|
+
if (equalsBy(comparer, element, value))
|
|
409
425
|
return true;
|
|
410
426
|
return false;
|
|
411
427
|
},
|
|
412
428
|
};
|
|
413
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
|
+
}
|
|
414
482
|
export function aggregate(seed, accumulator) {
|
|
415
483
|
return {
|
|
416
484
|
kind: "aggregate",
|
|
@@ -3,6 +3,7 @@ import type { Dictionary } from "../collections/dictionary.ts";
|
|
|
3
3
|
import type { HashSet } from "../collections/hash-set.ts";
|
|
4
4
|
import type { List } from "../collections/list.ts";
|
|
5
5
|
import type { QueryableMeta } from "./expr-tree.ts";
|
|
6
|
+
import type { Lookup } from "./lookup.ts";
|
|
6
7
|
/**
|
|
7
8
|
* Descriptor-based LINQ runtime — prototype for #20.
|
|
8
9
|
*
|
|
@@ -23,8 +24,8 @@ import type { QueryableMeta } from "./expr-tree.ts";
|
|
|
23
24
|
* Bundlers still tree-shake every unused operator: each factory is a
|
|
24
25
|
* standalone function and the union-type definition compiles away.
|
|
25
26
|
*/
|
|
26
|
-
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" | "defaultIfEmpty";
|
|
27
|
-
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" | "elementAt" | "elementAtOrDefault";
|
|
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";
|
|
28
29
|
/**
|
|
29
30
|
* Common shape every composition operator shares: a discriminator (`kind`)
|
|
30
31
|
* + a runtime walker (`apply`). Concrete descriptors extend this with the
|
|
@@ -159,10 +160,20 @@ export interface Grouping<K, T> extends Iterable<T> {
|
|
|
159
160
|
export interface GroupByOp<T, K> extends OperatorBase<T, Grouping<K, T>> {
|
|
160
161
|
readonly kind: "groupBy";
|
|
161
162
|
readonly keySelector: (item: T) => K;
|
|
163
|
+
readonly comparer?: IEqualityComparer<K> | null;
|
|
162
164
|
readonly queryable?: QueryableMeta;
|
|
163
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
|
+
}
|
|
164
175
|
/** Discriminated union of every composition operator. */
|
|
165
|
-
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> | DefaultIfEmptyOp<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>;
|
|
166
177
|
/**
|
|
167
178
|
* Common shape every terminal shares. `apply` walks the source and
|
|
168
179
|
* produces a concrete value; providers read `kind` + the captured
|
|
@@ -341,6 +352,18 @@ export interface MaxInt64Term<T, R extends bigint | null> extends TerminalBase<T
|
|
|
341
352
|
export interface ContainsTerm<T> extends TerminalBase<T, boolean> {
|
|
342
353
|
readonly kind: "contains";
|
|
343
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;
|
|
344
367
|
}
|
|
345
368
|
export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
|
|
346
369
|
readonly kind: "aggregate";
|
|
@@ -348,4 +371,4 @@ export interface AggregateTerm<T, A> extends TerminalBase<T, A> {
|
|
|
348
371
|
readonly accumulator: (acc: A, item: T) => A;
|
|
349
372
|
}
|
|
350
373
|
/** Discriminated union of every terminal. */
|
|
351
|
-
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> | 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,30 @@ 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;
|
|
63
|
+
/**
|
|
64
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
65
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
66
|
+
* stays `NaN`, as in .NET.
|
|
67
|
+
*
|
|
68
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function clampNumber(value: number, min: number, max: number): number;
|
|
71
|
+
/** Structural view of a `decimal.js` `Decimal`, the only operations {@link clampDecimal} uses. */
|
|
72
|
+
export interface DecimalClampable<D> {
|
|
73
|
+
lt(other: D): boolean;
|
|
74
|
+
gt(other: D): boolean;
|
|
75
|
+
toString(): string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
79
|
+
*
|
|
80
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
81
|
+
*/
|
|
82
|
+
export declare function clampDecimal<D extends DecimalClampable<D>>(value: D, min: D, max: D): D;
|
package/dist/system/math.js
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
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";
|
|
14
|
+
import { throwMinGreaterThanMax } from "./min-max.js";
|
|
13
15
|
/**
|
|
14
16
|
* Mirror of `System.MidpointRounding`. The numeric values match the .NET enum, so a
|
|
15
17
|
* value read from serialized C# data keeps its meaning.
|
|
@@ -139,3 +141,46 @@ function assertMode(mode) {
|
|
|
139
141
|
throw new ArgumentException(`The value '${mode}' is not valid for this usage of the type MidpointRounding.`, "mode");
|
|
140
142
|
}
|
|
141
143
|
}
|
|
144
|
+
const INT32_MIN = -2147483648;
|
|
145
|
+
/**
|
|
146
|
+
* `Math.Abs(int)` (#695): JavaScript's `Math.abs` returns `2147483648` for
|
|
147
|
+
* `int.MinValue`, whose magnitude does not fit `int`.
|
|
148
|
+
*
|
|
149
|
+
* @throws OverflowException, with .NET's message, for `int.MinValue`.
|
|
150
|
+
*/
|
|
151
|
+
export function absInt32(value) {
|
|
152
|
+
if (value === INT32_MIN) {
|
|
153
|
+
throw new OverflowException("Negating the minimum value of a twos complement number is invalid.");
|
|
154
|
+
}
|
|
155
|
+
return Math.abs(value);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* `Math.Clamp(value, min, max)` for the `int`, `short`, `byte`, `float`, `double` (and the
|
|
159
|
+
* other `number`-backed) overloads (#750); JavaScript has no `Math.clamp`. A `NaN` `value`
|
|
160
|
+
* stays `NaN`, as in .NET.
|
|
161
|
+
*
|
|
162
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
163
|
+
*/
|
|
164
|
+
export function clampNumber(value, min, max) {
|
|
165
|
+
if (min > max) {
|
|
166
|
+
throwMinGreaterThanMax(min, max);
|
|
167
|
+
}
|
|
168
|
+
if (value < min) {
|
|
169
|
+
return min;
|
|
170
|
+
}
|
|
171
|
+
return value > max ? max : value;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* `Math.Clamp(decimal, decimal, decimal)` (#750): the `Decimal` keeps its kind.
|
|
175
|
+
*
|
|
176
|
+
* @throws ArgumentException, with .NET's message, when `min` is greater than `max`.
|
|
177
|
+
*/
|
|
178
|
+
export function clampDecimal(value, min, max) {
|
|
179
|
+
if (min.gt(max)) {
|
|
180
|
+
throwMinGreaterThanMax(min, max);
|
|
181
|
+
}
|
|
182
|
+
if (value.lt(min)) {
|
|
183
|
+
return min;
|
|
184
|
+
}
|
|
185
|
+
return value.gt(max) ? max : value;
|
|
186
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
3
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
4
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
5
|
+
*/
|
|
6
|
+
export declare function throwMinGreaterThanMax(min: unknown, max: unknown): never;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
2
|
+
import { formatText } from "./default-text.js";
|
|
3
|
+
/**
|
|
4
|
+
* The `ArgumentException` of `Math.Clamp` and `INumber<T>.Clamp` when `min` is greater than
|
|
5
|
+
* `max`, with .NET's message. The bounds are formatted as .NET formats them (a `decimal`
|
|
6
|
+
* `0.0000001` stays `0.0000001`, a `double` follows `double.ToString()`).
|
|
7
|
+
*/
|
|
8
|
+
export function throwMinGreaterThanMax(min, max) {
|
|
9
|
+
throw new ArgumentException(`'${formatText(min)}' cannot be greater than ${formatText(max)}.`);
|
|
10
|
+
}
|
|
@@ -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
|
*
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* C#'s `string.Equals` and `string.Compare` with a `StringComparison` (#769), and the
|
|
3
|
+
* overloads that imply one (`Compare(a, b)` and `CompareTo` are culture-sensitive,
|
|
4
|
+
* `CompareOrdinal` is ordinal).
|
|
5
|
+
*
|
|
6
|
+
* - `Ordinal` compares UTF-16 code units.
|
|
7
|
+
* - `OrdinalIgnoreCase` compares the ordinal uppercase of each code point
|
|
8
|
+
* (`ordinalUppercaseOf`: .NET's invariant simple case mapping, so `ß` never equals
|
|
9
|
+
* `SS` and `ı` never equals `I`), not a whole-string `toLowerCase()`.
|
|
10
|
+
* - The culture members compare with `Intl.Collator`: `CurrentCulture` with the
|
|
11
|
+
* engine's default locale, `InvariantCulture` with the root locale, at `variant`
|
|
12
|
+
* sensitivity. The ignore-case members ignore case only, as .NET does on ICU: the
|
|
13
|
+
* strings first compare at `accent` sensitivity (which also ignores width and kana),
|
|
14
|
+
* and strings equal there are told apart by width and kana alone
|
|
15
|
+
* (`caseInsensitiveKey`), so `"A"` never equals `"a"` while `"A"` equals `"a"`,
|
|
16
|
+
* `ß` equals `ẞ` and `ff` equals `FF`. Measured against .NET 10 on ICU, five exotic
|
|
17
|
+
* pairs still differ (known gaps): `İ` / `i̇` and `ガ` / `ガ` and U+FFE8 / `│` (.NET
|
|
18
|
+
* equal), and small against full-size kana (`ア` / `ァ`, `ア` / `ァ`: the sign).
|
|
19
|
+
*
|
|
20
|
+
* The instance forms (`a.Equals(b, comparison)`, `a.Equals(b)`, `a.CompareTo(b)`) throw
|
|
21
|
+
* `NullReferenceException` for a `null` receiver, as .NET does; `CompareTo(object)`
|
|
22
|
+
* throws `ArgumentException` for an argument that is not a string.
|
|
23
|
+
*
|
|
24
|
+
* `null` equals only `null` and sorts before every string, as in .NET. A `Compare`
|
|
25
|
+
* result has .NET's sign; .NET leaves the magnitude unspecified, so the ordinal forms
|
|
26
|
+
* return the difference of the first code units that differ, or of the lengths.
|
|
27
|
+
*/
|
|
28
|
+
import { StringComparison } from "./string-members.ts";
|
|
29
|
+
/**
|
|
30
|
+
* C#'s `string.Equals(a, b, comparison)` and `a.Equals(b, comparison)`.
|
|
31
|
+
*
|
|
32
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
33
|
+
*/
|
|
34
|
+
export declare function stringEquals(left: string | null, right: string | null, comparison: StringComparison): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* C#'s `string.Compare(a, b, comparison)` and the overloads that imply a comparison:
|
|
37
|
+
* `Compare(a, b)`, `Compare(a, b, ignoreCase)`, `CompareOrdinal(a, b)`, `a.CompareTo(b)`.
|
|
38
|
+
*
|
|
39
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
40
|
+
*/
|
|
41
|
+
export declare function stringCompare(left: string | null, right: string | null, comparison: StringComparison): number;
|
|
42
|
+
/**
|
|
43
|
+
* C#'s `a.Equals(b, comparison)` and `a.Equals(b)` (ordinal).
|
|
44
|
+
*
|
|
45
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
46
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
47
|
+
*/
|
|
48
|
+
export declare function stringInstanceEquals(value: string | null, other: unknown, comparison: StringComparison): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* C#'s `a.CompareTo(b)` for a `string` or `object` argument: a culture-sensitive
|
|
51
|
+
* comparison; `null` sorts first.
|
|
52
|
+
*
|
|
53
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
54
|
+
* @throws ArgumentException when `other` is neither `null` nor a string.
|
|
55
|
+
*/
|
|
56
|
+
export declare function stringCompareTo(value: string | null, other: unknown): number;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* C#'s `string.Equals` and `string.Compare` with a `StringComparison` (#769), and the
|
|
3
|
+
* overloads that imply one (`Compare(a, b)` and `CompareTo` are culture-sensitive,
|
|
4
|
+
* `CompareOrdinal` is ordinal).
|
|
5
|
+
*
|
|
6
|
+
* - `Ordinal` compares UTF-16 code units.
|
|
7
|
+
* - `OrdinalIgnoreCase` compares the ordinal uppercase of each code point
|
|
8
|
+
* (`ordinalUppercaseOf`: .NET's invariant simple case mapping, so `ß` never equals
|
|
9
|
+
* `SS` and `ı` never equals `I`), not a whole-string `toLowerCase()`.
|
|
10
|
+
* - The culture members compare with `Intl.Collator`: `CurrentCulture` with the
|
|
11
|
+
* engine's default locale, `InvariantCulture` with the root locale, at `variant`
|
|
12
|
+
* sensitivity. The ignore-case members ignore case only, as .NET does on ICU: the
|
|
13
|
+
* strings first compare at `accent` sensitivity (which also ignores width and kana),
|
|
14
|
+
* and strings equal there are told apart by width and kana alone
|
|
15
|
+
* (`caseInsensitiveKey`), so `"A"` never equals `"a"` while `"A"` equals `"a"`,
|
|
16
|
+
* `ß` equals `ẞ` and `ff` equals `FF`. Measured against .NET 10 on ICU, five exotic
|
|
17
|
+
* pairs still differ (known gaps): `İ` / `i̇` and `ガ` / `ガ` and U+FFE8 / `│` (.NET
|
|
18
|
+
* equal), and small against full-size kana (`ア` / `ァ`, `ア` / `ァ`: the sign).
|
|
19
|
+
*
|
|
20
|
+
* The instance forms (`a.Equals(b, comparison)`, `a.Equals(b)`, `a.CompareTo(b)`) throw
|
|
21
|
+
* `NullReferenceException` for a `null` receiver, as .NET does; `CompareTo(object)`
|
|
22
|
+
* throws `ArgumentException` for an argument that is not a string.
|
|
23
|
+
*
|
|
24
|
+
* `null` equals only `null` and sorts before every string, as in .NET. A `Compare`
|
|
25
|
+
* result has .NET's sign; .NET leaves the magnitude unspecified, so the ordinal forms
|
|
26
|
+
* return the difference of the first code units that differ, or of the lengths.
|
|
27
|
+
*/
|
|
28
|
+
import { ArgumentException } from "./exceptions/argument-exception.js";
|
|
29
|
+
import { NullReferenceException } from "./exceptions/null-reference-exception.js";
|
|
30
|
+
import { ordinalUppercaseOf } from "./json/ordinal-ignore-case.js";
|
|
31
|
+
import { StringComparison } from "./string-members.js";
|
|
32
|
+
const UNSUPPORTED_COMPARISON_MESSAGE = "The string comparison type passed in is currently not supported.";
|
|
33
|
+
const NOT_A_STRING_MESSAGE = "Object must be of type String.";
|
|
34
|
+
const FIRST_SUPPLEMENTARY_CODE_POINT = 0x10000;
|
|
35
|
+
/* Built on first use, one per culture member, so importing the module runs no code. */
|
|
36
|
+
const cultureCollators = [];
|
|
37
|
+
/** The collator for a culture member (`CurrentCulture` to `InvariantCultureIgnoreCase`). */
|
|
38
|
+
function collatorFor(comparison) {
|
|
39
|
+
const isInvariant = comparison >= StringComparison.InvariantCulture;
|
|
40
|
+
const ignoresCase = comparison % 2 === 1;
|
|
41
|
+
return (cultureCollators[comparison] ??= new Intl.Collator(isInvariant ? "und" : undefined, {
|
|
42
|
+
sensitivity: ignoresCase ? "accent" : "variant",
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
/** @throws ArgumentException when `comparison` is not a `StringComparison` member. */
|
|
46
|
+
function requireComparison(comparison) {
|
|
47
|
+
const isMember = Number.isInteger(comparison) &&
|
|
48
|
+
comparison >= StringComparison.CurrentCulture &&
|
|
49
|
+
comparison <= StringComparison.OrdinalIgnoreCase;
|
|
50
|
+
if (!isMember) {
|
|
51
|
+
throw new ArgumentException(UNSUPPORTED_COMPARISON_MESSAGE, "comparisonType");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The ordinal uppercase code point of the code point at `index` in `value`. */
|
|
55
|
+
function foldedCodePointAt(value, index) {
|
|
56
|
+
const unit = value.charCodeAt(index);
|
|
57
|
+
if (unit < 0x80) {
|
|
58
|
+
return unit >= 0x61 && unit <= 0x7a ? unit - 0x20 : unit;
|
|
59
|
+
}
|
|
60
|
+
const codePoint = value.codePointAt(index);
|
|
61
|
+
return ordinalUppercaseOf(String.fromCodePoint(codePoint)).codePointAt(0);
|
|
62
|
+
}
|
|
63
|
+
function compareOrdinal(left, right) {
|
|
64
|
+
const length = Math.min(left.length, right.length);
|
|
65
|
+
for (let index = 0; index < length; index++) {
|
|
66
|
+
const difference = left.charCodeAt(index) - right.charCodeAt(index);
|
|
67
|
+
if (difference !== 0) {
|
|
68
|
+
return difference;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return left.length - right.length;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Walks both strings code point by code point; a supplementary code point orders by its
|
|
75
|
+
* value (above every BMP one), as .NET's case-insensitive comparison does.
|
|
76
|
+
*/
|
|
77
|
+
function compareOrdinalIgnoreCase(left, right) {
|
|
78
|
+
let leftIndex = 0;
|
|
79
|
+
let rightIndex = 0;
|
|
80
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
81
|
+
const leftFolded = foldedCodePointAt(left, leftIndex);
|
|
82
|
+
const rightFolded = foldedCodePointAt(right, rightIndex);
|
|
83
|
+
if (leftFolded !== rightFolded) {
|
|
84
|
+
const isSupplementary = leftFolded >= FIRST_SUPPLEMENTARY_CODE_POINT ||
|
|
85
|
+
rightFolded >= FIRST_SUPPLEMENTARY_CODE_POINT;
|
|
86
|
+
return isSupplementary ? Math.sign(leftFolded - rightFolded) : leftFolded - rightFolded;
|
|
87
|
+
}
|
|
88
|
+
leftIndex += left.codePointAt(leftIndex) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
|
|
89
|
+
rightIndex +=
|
|
90
|
+
right.codePointAt(rightIndex) >= FIRST_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
|
|
91
|
+
}
|
|
92
|
+
return left.length - leftIndex - (right.length - rightIndex);
|
|
93
|
+
}
|
|
94
|
+
/** Two non-null strings under `comparison`, already checked. */
|
|
95
|
+
function compareWith(left, right, comparison) {
|
|
96
|
+
switch (comparison) {
|
|
97
|
+
case StringComparison.Ordinal:
|
|
98
|
+
return compareOrdinal(left, right);
|
|
99
|
+
case StringComparison.OrdinalIgnoreCase:
|
|
100
|
+
return compareOrdinalIgnoreCase(left, right);
|
|
101
|
+
case StringComparison.CurrentCultureIgnoreCase:
|
|
102
|
+
case StringComparison.InvariantCultureIgnoreCase:
|
|
103
|
+
return compareCultureIgnoringCase(left, right, comparison);
|
|
104
|
+
default:
|
|
105
|
+
return collatorFor(comparison).compare(left, right);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* .NET's culture `IgnoreCase` on ICU ignores case but not width (`A` / `a`, `カ` / `カ`)
|
|
110
|
+
* or kana type (`あ` / `ア`), which `accent` sensitivity ignores too. Strings that differ
|
|
111
|
+
* at `accent` sensitivity keep that order; strings equal there compare their
|
|
112
|
+
* case-insensitive keys at `variant` sensitivity, where only width and kana remain.
|
|
113
|
+
*/
|
|
114
|
+
function compareCultureIgnoringCase(left, right, comparison) {
|
|
115
|
+
const primary = collatorFor(comparison).compare(left, right);
|
|
116
|
+
if (primary !== 0) {
|
|
117
|
+
return primary;
|
|
118
|
+
}
|
|
119
|
+
const caseSensitive = (comparison - 1);
|
|
120
|
+
return collatorFor(caseSensitive).compare(caseInsensitiveKey(left), caseInsensitiveKey(right));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* `value` with every difference .NET's culture `IgnoreCase` ignores removed: each code
|
|
124
|
+
* point outside the halfwidth and fullwidth forms is replaced by its compatibility
|
|
125
|
+
* decomposition (`ff` → `ff`, `①` → `1`, U+3000 → space) in a single case (simple
|
|
126
|
+
* uppercase, then simple lowercase: `ς` and `Σ` → `σ`, `ẞ` → `ß`). A width form keeps
|
|
127
|
+
* its width and only folds its case (`A` → `a`).
|
|
128
|
+
*/
|
|
129
|
+
function caseInsensitiveKey(value) {
|
|
130
|
+
let key = "";
|
|
131
|
+
for (const character of value) {
|
|
132
|
+
if (isWidthForm(character.codePointAt(0))) {
|
|
133
|
+
key += singleCase(character);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
for (const part of character.normalize("NFKC")) {
|
|
137
|
+
key += singleCase(part);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return key;
|
|
141
|
+
}
|
|
142
|
+
/** The Halfwidth and Fullwidth Forms block. */
|
|
143
|
+
function isWidthForm(codePoint) {
|
|
144
|
+
return codePoint >= 0xff00 && codePoint <= 0xffef;
|
|
145
|
+
}
|
|
146
|
+
function singleCase(character) {
|
|
147
|
+
const upper = simpleMapping(character, character.toUpperCase());
|
|
148
|
+
return simpleMapping(upper, upper.toLowerCase());
|
|
149
|
+
}
|
|
150
|
+
/** `mapped` when it is one code point (the simple mapping), else `character`. */
|
|
151
|
+
function simpleMapping(character, mapped) {
|
|
152
|
+
return mapped.length === character.length || [...mapped].length === 1 ? mapped : character;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* C#'s `string.Equals(a, b, comparison)` and `a.Equals(b, comparison)`.
|
|
156
|
+
*
|
|
157
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
158
|
+
*/
|
|
159
|
+
export function stringEquals(left, right, comparison) {
|
|
160
|
+
requireComparison(comparison);
|
|
161
|
+
if (left === right) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (left === null || right === null || comparison === StringComparison.Ordinal) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
if (comparison === StringComparison.OrdinalIgnoreCase && left.length !== right.length) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
return compareWith(left, right, comparison) === 0;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* C#'s `string.Compare(a, b, comparison)` and the overloads that imply a comparison:
|
|
174
|
+
* `Compare(a, b)`, `Compare(a, b, ignoreCase)`, `CompareOrdinal(a, b)`, `a.CompareTo(b)`.
|
|
175
|
+
*
|
|
176
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
177
|
+
*/
|
|
178
|
+
export function stringCompare(left, right, comparison) {
|
|
179
|
+
requireComparison(comparison);
|
|
180
|
+
if (left === right) {
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
if (left === null) {
|
|
184
|
+
return -1;
|
|
185
|
+
}
|
|
186
|
+
if (right === null) {
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
return compareWith(left, right, comparison);
|
|
190
|
+
}
|
|
191
|
+
/** @throws NullReferenceException when the receiver of an instance call is `null`. */
|
|
192
|
+
function requireReceiver(value) {
|
|
193
|
+
if (value == null) {
|
|
194
|
+
throw new NullReferenceException();
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* C#'s `a.Equals(b, comparison)` and `a.Equals(b)` (ordinal).
|
|
200
|
+
*
|
|
201
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
202
|
+
* @throws ArgumentException when `comparison` is not a `StringComparison` member.
|
|
203
|
+
*/
|
|
204
|
+
export function stringInstanceEquals(value, other, comparison) {
|
|
205
|
+
const receiver = requireReceiver(value);
|
|
206
|
+
if (other === null || other === undefined || typeof other === "string") {
|
|
207
|
+
return stringEquals(receiver, other ?? null, comparison);
|
|
208
|
+
}
|
|
209
|
+
requireComparison(comparison);
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* C#'s `a.CompareTo(b)` for a `string` or `object` argument: a culture-sensitive
|
|
214
|
+
* comparison; `null` sorts first.
|
|
215
|
+
*
|
|
216
|
+
* @throws NullReferenceException when `value` is `null`.
|
|
217
|
+
* @throws ArgumentException when `other` is neither `null` nor a string.
|
|
218
|
+
*/
|
|
219
|
+
export function stringCompareTo(value, other) {
|
|
220
|
+
const receiver = requireReceiver(value);
|
|
221
|
+
if (other !== null && other !== undefined && typeof other !== "string") {
|
|
222
|
+
throw new ArgumentException(NOT_A_STRING_MESSAGE);
|
|
223
|
+
}
|
|
224
|
+
return stringCompare(receiver, other ?? null, StringComparison.CurrentCulture);
|
|
225
|
+
}
|