metano-runtime 2.7.0 → 2.7.2
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/collections/dictionary-helpers.d.ts +13 -0
- package/dist/system/collections/dictionary-helpers.js +21 -0
- package/dist/system/equality.d.ts +30 -0
- package/dist/system/equality.js +59 -1
- package/dist/system/index.d.ts +1 -0
- package/dist/system/index.js +1 -0
- package/dist/system/strings.d.ts +38 -0
- package/dist/system/strings.js +67 -0
- package/package.json +1 -1
|
@@ -25,3 +25,16 @@ export declare function getValueOrDefault<K, V>(map: ReadonlyMap<K, V>, key: K,
|
|
|
25
25
|
* `found`.
|
|
26
26
|
*/
|
|
27
27
|
export declare function tryGetValue<K, V, D>(map: ReadonlyMap<K, V>, key: K, missingValue: D): readonly [found: true, value: V] | readonly [found: false, value: D];
|
|
28
|
+
/**
|
|
29
|
+
* Adds `value` under `key`, mirroring `Dictionary<TKey, TValue>.Add(key, value)`: unlike
|
|
30
|
+
* `Map.set`, an existing entry is never overwritten. Collection initializers
|
|
31
|
+
* (`new Dictionary<K, V> { { k, v } }`) lower to this call too.
|
|
32
|
+
*
|
|
33
|
+
* The key is rendered with `String(key)`, which matches the .NET message for
|
|
34
|
+
* primitive keys and for keys whose class overrides `toString`.
|
|
35
|
+
*
|
|
36
|
+
* @throws Error when `key` is `null` or `undefined` (.NET throws
|
|
37
|
+
* `ArgumentNullException`) or is already present (.NET throws `ArgumentException`),
|
|
38
|
+
* with the message .NET reports.
|
|
39
|
+
*/
|
|
40
|
+
export declare function addEntry<K, V>(map: Map<K, V>, key: K, value: V): void;
|
|
@@ -29,3 +29,24 @@ export function getValueOrDefault(map, key, defaultValue) {
|
|
|
29
29
|
export function tryGetValue(map, key, missingValue) {
|
|
30
30
|
return map.has(key) ? [true, map.get(key)] : [false, missingValue];
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Adds `value` under `key`, mirroring `Dictionary<TKey, TValue>.Add(key, value)`: unlike
|
|
34
|
+
* `Map.set`, an existing entry is never overwritten. Collection initializers
|
|
35
|
+
* (`new Dictionary<K, V> { { k, v } }`) lower to this call too.
|
|
36
|
+
*
|
|
37
|
+
* The key is rendered with `String(key)`, which matches the .NET message for
|
|
38
|
+
* primitive keys and for keys whose class overrides `toString`.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error when `key` is `null` or `undefined` (.NET throws
|
|
41
|
+
* `ArgumentNullException`) or is already present (.NET throws `ArgumentException`),
|
|
42
|
+
* with the message .NET reports.
|
|
43
|
+
*/
|
|
44
|
+
export function addEntry(map, key, value) {
|
|
45
|
+
if (key === null || key === undefined) {
|
|
46
|
+
throw new Error("Value cannot be null. (Parameter 'key')");
|
|
47
|
+
}
|
|
48
|
+
if (map.has(key)) {
|
|
49
|
+
throw new Error(`An item with the same key has already been added. Key: ${String(key)}`);
|
|
50
|
+
}
|
|
51
|
+
map.set(key, value);
|
|
52
|
+
}
|
|
@@ -44,3 +44,33 @@ export declare function hashCode(value: unknown): number;
|
|
|
44
44
|
* recursive method-call ambiguity for human readers).
|
|
45
45
|
*/
|
|
46
46
|
export declare const valueEquals: typeof equals;
|
|
47
|
+
/**
|
|
48
|
+
* C#'s static `object.Equals(a, b)`: true when both are null or `a.Equals(b)`, where
|
|
49
|
+
* `Equals` is what the value's type defines. Unlike {@link equals}, it never compares a
|
|
50
|
+
* class instance by structure, since a C# class that does not override `Equals`
|
|
51
|
+
* compares by reference:
|
|
52
|
+
*
|
|
53
|
+
* 1. Reference identity (`a === b`) wins immediately; `null` / `undefined` equal only
|
|
54
|
+
* themselves.
|
|
55
|
+
* 2. `NaN` equals `NaN`; other primitives use `===`.
|
|
56
|
+
* 3. Objects with an `equals(other)` method delegate to it (records, user overrides,
|
|
57
|
+
* `Decimal`, Temporal types).
|
|
58
|
+
* 4. Plain object literals (`[PlainObject]` records, anonymous types) compare by shape
|
|
59
|
+
* through {@link equals}, as their C# types compare by value.
|
|
60
|
+
* 5. Everything else (class instances, arrays, `Map`s, `Set`s) compares by reference,
|
|
61
|
+
* as C# arrays, lists, dictionaries and sets do.
|
|
62
|
+
*/
|
|
63
|
+
export declare function objectEquals(a: unknown, b: unknown): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* C#'s `ValueTuple.Equals(other)` over the positional arrays C# tuples lower to: same
|
|
66
|
+
* length, and each element pair equal under {@link objectEquals} (C# compares each
|
|
67
|
+
* element with `EqualityComparer<T>.Default`, so a class element compares by reference).
|
|
68
|
+
* A nested tuple element is an array too, so it compares by reference.
|
|
69
|
+
*/
|
|
70
|
+
export declare function tupleEquals(tuple: readonly unknown[], other: unknown): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Alias the compiler emits for every C# `GetHashCode()` call. Same contract as
|
|
73
|
+
* {@link hashCode}; the distinct name keeps a call site from colliding with a
|
|
74
|
+
* local or a method the user named `hashCode` (records declare one).
|
|
75
|
+
*/
|
|
76
|
+
export declare const valueHashCode: typeof hashCode;
|
package/dist/system/equality.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* `equals` methods whenever a constructor parameter has a non-primitive type that
|
|
8
8
|
* exposes a value-equality contract (Decimal, Temporal types, nested records, …).
|
|
9
9
|
* The previous lowering used `===` for every field which silently diverged from C#
|
|
10
|
-
* semantics for value-wrapper types — see #202.
|
|
10
|
+
* semantics for value-wrapper types — see #202. It also lowers C# `==` / `!=` over
|
|
11
|
+
* records to {@link valueEquals}, and every C# `GetHashCode()` call to
|
|
12
|
+
* {@link valueHashCode} (#254).
|
|
11
13
|
*/
|
|
12
14
|
import { HashCode } from "./hash-code.js";
|
|
13
15
|
/**
|
|
@@ -184,3 +186,59 @@ export function hashCode(value) {
|
|
|
184
186
|
* recursive method-call ambiguity for human readers).
|
|
185
187
|
*/
|
|
186
188
|
export const valueEquals = equals;
|
|
189
|
+
/**
|
|
190
|
+
* C#'s static `object.Equals(a, b)`: true when both are null or `a.Equals(b)`, where
|
|
191
|
+
* `Equals` is what the value's type defines. Unlike {@link equals}, it never compares a
|
|
192
|
+
* class instance by structure, since a C# class that does not override `Equals`
|
|
193
|
+
* compares by reference:
|
|
194
|
+
*
|
|
195
|
+
* 1. Reference identity (`a === b`) wins immediately; `null` / `undefined` equal only
|
|
196
|
+
* themselves.
|
|
197
|
+
* 2. `NaN` equals `NaN`; other primitives use `===`.
|
|
198
|
+
* 3. Objects with an `equals(other)` method delegate to it (records, user overrides,
|
|
199
|
+
* `Decimal`, Temporal types).
|
|
200
|
+
* 4. Plain object literals (`[PlainObject]` records, anonymous types) compare by shape
|
|
201
|
+
* through {@link equals}, as their C# types compare by value.
|
|
202
|
+
* 5. Everything else (class instances, arrays, `Map`s, `Set`s) compares by reference,
|
|
203
|
+
* as C# arrays, lists, dictionaries and sets do.
|
|
204
|
+
*/
|
|
205
|
+
export function objectEquals(a, b) {
|
|
206
|
+
if (a === b)
|
|
207
|
+
return true;
|
|
208
|
+
if (a == null || b == null)
|
|
209
|
+
return false;
|
|
210
|
+
if (typeof a === "number") {
|
|
211
|
+
return typeof b === "number" && Number.isNaN(a) && Number.isNaN(b);
|
|
212
|
+
}
|
|
213
|
+
if (typeof a !== "object")
|
|
214
|
+
return false;
|
|
215
|
+
if (hasEqualsMethod(a))
|
|
216
|
+
return a.equals(b);
|
|
217
|
+
return isPlainObject(a) && isPlainObject(b) && equals(a, b);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* C#'s `ValueTuple.Equals(other)` over the positional arrays C# tuples lower to: same
|
|
221
|
+
* length, and each element pair equal under {@link objectEquals} (C# compares each
|
|
222
|
+
* element with `EqualityComparer<T>.Default`, so a class element compares by reference).
|
|
223
|
+
* A nested tuple element is an array too, so it compares by reference.
|
|
224
|
+
*/
|
|
225
|
+
export function tupleEquals(tuple, other) {
|
|
226
|
+
if (!Array.isArray(other) || other.length !== tuple.length)
|
|
227
|
+
return false;
|
|
228
|
+
return tuple.every((element, index) => objectEquals(element, other[index]));
|
|
229
|
+
}
|
|
230
|
+
function hasEqualsMethod(value) {
|
|
231
|
+
return "equals" in value && typeof value.equals === "function";
|
|
232
|
+
}
|
|
233
|
+
function isPlainObject(value) {
|
|
234
|
+
if (typeof value !== "object" || value === null)
|
|
235
|
+
return false;
|
|
236
|
+
const prototype = Object.getPrototypeOf(value);
|
|
237
|
+
return prototype === Object.prototype || prototype === null;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Alias the compiler emits for every C# `GetHashCode()` call. Same contract as
|
|
241
|
+
* {@link hashCode}; the distinct name keeps a call site from colliding with a
|
|
242
|
+
* local or a method the user named `hashCode` (records declare one).
|
|
243
|
+
*/
|
|
244
|
+
export const valueHashCode = hashCode;
|
package/dist/system/index.d.ts
CHANGED
package/dist/system/index.js
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime helpers for `System.String` members whose C# semantics differ from the
|
|
3
|
+
* closest JavaScript built-in.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* C#'s `string.IsNullOrEmpty(value)`: `true` for `null`, `undefined` and `""`.
|
|
7
|
+
*
|
|
8
|
+
* The type predicate lets a `false` result narrow `value` to `string`, as C#'s
|
|
9
|
+
* `[NotNullWhen(false)]` does.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isNullOrEmpty(value: string | null | undefined): value is "" | null | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* What a `true` {@link isNullOrWhiteSpace} result narrows its argument to: a missing
|
|
14
|
+
* value or a string the brand marks as white space only. The brand exists only in the
|
|
15
|
+
* type system; it keeps the narrowed string comparable with any literal (`s === " "`).
|
|
16
|
+
*/
|
|
17
|
+
export type NullOrWhiteSpace = (string & {
|
|
18
|
+
readonly __brand: "WhiteSpace";
|
|
19
|
+
}) | null | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* C#'s `string.IsNullOrWhiteSpace(value)`: `true` for `null`, `undefined`, `""` and a
|
|
22
|
+
* string made only of .NET white-space characters.
|
|
23
|
+
*
|
|
24
|
+
* The type predicate lets a `false` result narrow `value` to `string`, as C#'s
|
|
25
|
+
* `[NotNullWhen(false)]` does.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isNullOrWhiteSpace(value: string | null | undefined): value is NullOrWhiteSpace;
|
|
28
|
+
/**
|
|
29
|
+
* C#'s `value.Replace(oldValue, newValue)` for the `(char, char)` and
|
|
30
|
+
* `(string, string?)` overloads: replaces every ordinal occurrence of `oldValue`,
|
|
31
|
+
* scanning left to right without overlap. Unlike `String.prototype.replace`, it
|
|
32
|
+
* replaces all occurrences, and unlike `replaceAll`, `$` sequences in `newValue` are
|
|
33
|
+
* inserted literally. A `null` `newValue` removes the occurrences.
|
|
34
|
+
*
|
|
35
|
+
* @throws Error with .NET's `ArgumentNullException` message when `oldValue` is `null`,
|
|
36
|
+
* and with its `ArgumentException` message when `oldValue` is empty.
|
|
37
|
+
*/
|
|
38
|
+
export declare function stringReplace(value: string, oldValue: string, newValue: string | null | undefined): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime helpers for `System.String` members whose C# semantics differ from the
|
|
3
|
+
* closest JavaScript built-in.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The single characters .NET's `char.IsWhiteSpace` accepts besides the ranges tested in
|
|
7
|
+
* {@link isDotNetWhiteSpace}. JavaScript's `trim()` and `\s` disagree with .NET on two
|
|
8
|
+
* code points: they accept U+FEFF, which .NET does not, and reject U+0085, which .NET
|
|
9
|
+
* accepts.
|
|
10
|
+
*/
|
|
11
|
+
const DOTNET_WHITE_SPACE_CODES = new Set([
|
|
12
|
+
0x0020, 0x0085, 0x00a0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000,
|
|
13
|
+
]);
|
|
14
|
+
/** Whether `code` is a .NET white-space character (`char.IsWhiteSpace`). */
|
|
15
|
+
function isDotNetWhiteSpace(code) {
|
|
16
|
+
const isControlWhiteSpace = code >= 0x0009 && code <= 0x000d;
|
|
17
|
+
const isSpaceSeparatorRange = code >= 0x2000 && code <= 0x200a;
|
|
18
|
+
return isControlWhiteSpace || isSpaceSeparatorRange || DOTNET_WHITE_SPACE_CODES.has(code);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* C#'s `string.IsNullOrEmpty(value)`: `true` for `null`, `undefined` and `""`.
|
|
22
|
+
*
|
|
23
|
+
* The type predicate lets a `false` result narrow `value` to `string`, as C#'s
|
|
24
|
+
* `[NotNullWhen(false)]` does.
|
|
25
|
+
*/
|
|
26
|
+
export function isNullOrEmpty(value) {
|
|
27
|
+
return value == null || value.length === 0;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* C#'s `string.IsNullOrWhiteSpace(value)`: `true` for `null`, `undefined`, `""` and a
|
|
31
|
+
* string made only of .NET white-space characters.
|
|
32
|
+
*
|
|
33
|
+
* The type predicate lets a `false` result narrow `value` to `string`, as C#'s
|
|
34
|
+
* `[NotNullWhen(false)]` does.
|
|
35
|
+
*/
|
|
36
|
+
export function isNullOrWhiteSpace(value) {
|
|
37
|
+
if (value == null) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
for (let index = 0; index < value.length; index++) {
|
|
41
|
+
if (!isDotNetWhiteSpace(value.charCodeAt(index))) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* C#'s `value.Replace(oldValue, newValue)` for the `(char, char)` and
|
|
49
|
+
* `(string, string?)` overloads: replaces every ordinal occurrence of `oldValue`,
|
|
50
|
+
* scanning left to right without overlap. Unlike `String.prototype.replace`, it
|
|
51
|
+
* replaces all occurrences, and unlike `replaceAll`, `$` sequences in `newValue` are
|
|
52
|
+
* inserted literally. A `null` `newValue` removes the occurrences.
|
|
53
|
+
*
|
|
54
|
+
* @throws Error with .NET's `ArgumentNullException` message when `oldValue` is `null`,
|
|
55
|
+
* and with its `ArgumentException` message when `oldValue` is empty.
|
|
56
|
+
*/
|
|
57
|
+
export function stringReplace(value, oldValue, newValue) {
|
|
58
|
+
// Generated code is not always type-checked against this signature (a C# `null!`
|
|
59
|
+
// argument reaches here), so the C# null check stays.
|
|
60
|
+
if (oldValue == null) {
|
|
61
|
+
throw new Error("Value cannot be null. (Parameter 'oldValue')");
|
|
62
|
+
}
|
|
63
|
+
if (oldValue.length === 0) {
|
|
64
|
+
throw new Error("The value cannot be an empty string. (Parameter 'oldValue')");
|
|
65
|
+
}
|
|
66
|
+
return value.split(oldValue).join(newValue ?? "");
|
|
67
|
+
}
|