metano-runtime 2.7.0 → 2.7.1
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.
|
@@ -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;
|