@evolu/common 8.7.0 → 8.9.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/src/Array.d.ts +21 -21
- package/dist/src/Array.js +5 -5
- package/dist/src/Assert.d.ts +498 -141
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +648 -99
- package/dist/src/{Buffer.d.ts → Binary.d.ts} +85 -12
- package/dist/src/Binary.d.ts.map +1 -0
- package/dist/src/{Buffer.js → Binary.js} +206 -24
- package/dist/src/Console.d.ts +2 -10
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +6 -12
- package/dist/src/Crypto.d.ts +5 -5
- package/dist/src/Crypto.js +1 -1
- package/dist/src/Eq.d.ts +35 -40
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +51 -87
- package/dist/src/Function.d.ts +9 -14
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +7 -9
- package/dist/src/Http.d.ts +6 -7
- package/dist/src/Http.d.ts.map +1 -1
- package/dist/src/LeakDetector.d.ts.map +1 -1
- package/dist/src/LeakDetector.js +1 -0
- package/dist/src/LockManager.d.ts +1 -1
- package/dist/src/LockManager.js +1 -1
- package/dist/src/Lookup.d.ts +3 -2
- package/dist/src/Lookup.d.ts.map +1 -1
- package/dist/src/Lookup.js +4 -3
- package/dist/src/Number.d.ts +1 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +1 -1
- package/dist/src/Object.d.ts +4 -4
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +5 -4
- package/dist/src/Option.d.ts +1 -2
- package/dist/src/Option.d.ts.map +1 -1
- package/dist/src/Option.js +1 -2
- package/dist/src/Platform.js +2 -0
- package/dist/src/Polyfills.d.ts +6 -0
- package/dist/src/Polyfills.d.ts.map +1 -1
- package/dist/src/Polyfills.js +10 -1
- package/dist/src/Random.d.ts +1 -1
- package/dist/src/Result.d.ts +49 -54
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +17 -22
- package/dist/src/Set.d.ts +6 -6
- package/dist/src/Set.js +2 -2
- package/dist/src/Sqlite.js +1 -1
- package/dist/src/String.js +3 -2
- package/dist/src/Task.d.ts +32 -32
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +20 -22
- package/dist/src/Test.d.ts +31 -3
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +46 -3
- package/dist/src/Time.d.ts +3 -11
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +10 -18
- package/dist/src/Type.d.ts +87 -85
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +70 -63
- package/dist/src/Types.d.ts +12 -12
- package/dist/src/Types.js +1 -1
- package/dist/src/WebSocket.d.ts +4 -7
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/Worker.d.ts +1 -1
- package/dist/src/Worker.js +7 -7
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/intl/_en.d.ts.map +1 -1
- package/dist/src/intl/_en.js +1 -1
- package/dist/src/intl/ar.d.ts.map +1 -1
- package/dist/src/intl/ar.js +1 -1
- package/dist/src/intl/bn.d.ts.map +1 -1
- package/dist/src/intl/bn.js +1 -1
- package/dist/src/intl/ca.d.ts.map +1 -1
- package/dist/src/intl/ca.js +1 -1
- package/dist/src/intl/cs.d.ts.map +1 -1
- package/dist/src/intl/cs.js +1 -1
- package/dist/src/intl/da.d.ts.map +1 -1
- package/dist/src/intl/da.js +1 -1
- package/dist/src/intl/de.d.ts.map +1 -1
- package/dist/src/intl/de.js +1 -1
- package/dist/src/intl/el.d.ts.map +1 -1
- package/dist/src/intl/el.js +1 -1
- package/dist/src/intl/es.d.ts.map +1 -1
- package/dist/src/intl/es.js +1 -1
- package/dist/src/intl/fa.d.ts.map +1 -1
- package/dist/src/intl/fa.js +1 -1
- package/dist/src/intl/fi.d.ts.map +1 -1
- package/dist/src/intl/fi.js +1 -1
- package/dist/src/intl/fil.d.ts.map +1 -1
- package/dist/src/intl/fil.js +1 -1
- package/dist/src/intl/fr.d.ts.map +1 -1
- package/dist/src/intl/fr.js +1 -1
- package/dist/src/intl/he.d.ts.map +1 -1
- package/dist/src/intl/he.js +1 -1
- package/dist/src/intl/hi.d.ts.map +1 -1
- package/dist/src/intl/hi.js +1 -1
- package/dist/src/intl/hr.d.ts.map +1 -1
- package/dist/src/intl/hr.js +1 -1
- package/dist/src/intl/hu.d.ts.map +1 -1
- package/dist/src/intl/hu.js +1 -1
- package/dist/src/intl/id.d.ts.map +1 -1
- package/dist/src/intl/id.js +1 -1
- package/dist/src/intl/it.d.ts.map +1 -1
- package/dist/src/intl/it.js +1 -1
- package/dist/src/intl/ja.d.ts.map +1 -1
- package/dist/src/intl/ja.js +1 -1
- package/dist/src/intl/ko.d.ts.map +1 -1
- package/dist/src/intl/ko.js +1 -1
- package/dist/src/intl/ml.d.ts.map +1 -1
- package/dist/src/intl/ml.js +1 -1
- package/dist/src/intl/mr.d.ts.map +1 -1
- package/dist/src/intl/mr.js +1 -1
- package/dist/src/intl/ms.d.ts.map +1 -1
- package/dist/src/intl/ms.js +1 -1
- package/dist/src/intl/nb.d.ts.map +1 -1
- package/dist/src/intl/nb.js +1 -1
- package/dist/src/intl/nl.d.ts.map +1 -1
- package/dist/src/intl/nl.js +1 -1
- package/dist/src/intl/pa.d.ts.map +1 -1
- package/dist/src/intl/pa.js +1 -1
- package/dist/src/intl/pl.d.ts.map +1 -1
- package/dist/src/intl/pl.js +1 -1
- package/dist/src/intl/pt-BR.d.ts.map +1 -1
- package/dist/src/intl/pt-BR.js +1 -1
- package/dist/src/intl/pt.d.ts.map +1 -1
- package/dist/src/intl/pt.js +1 -1
- package/dist/src/intl/ro.d.ts.map +1 -1
- package/dist/src/intl/ro.js +1 -1
- package/dist/src/intl/sk.d.ts.map +1 -1
- package/dist/src/intl/sk.js +1 -1
- package/dist/src/intl/sl.d.ts.map +1 -1
- package/dist/src/intl/sl.js +1 -1
- package/dist/src/intl/sv.d.ts.map +1 -1
- package/dist/src/intl/sv.js +1 -1
- package/dist/src/intl/sw.d.ts.map +1 -1
- package/dist/src/intl/sw.js +1 -1
- package/dist/src/intl/ta.d.ts.map +1 -1
- package/dist/src/intl/ta.js +1 -1
- package/dist/src/intl/te.d.ts.map +1 -1
- package/dist/src/intl/te.js +1 -1
- package/dist/src/intl/th.d.ts.map +1 -1
- package/dist/src/intl/th.js +1 -1
- package/dist/src/intl/tr.d.ts.map +1 -1
- package/dist/src/intl/tr.js +1 -1
- package/dist/src/intl/uk.d.ts.map +1 -1
- package/dist/src/intl/uk.js +1 -1
- package/dist/src/intl/ur.d.ts.map +1 -1
- package/dist/src/intl/ur.js +1 -1
- package/dist/src/intl/vi.d.ts.map +1 -1
- package/dist/src/intl/vi.js +1 -1
- package/dist/src/intl/zh-CN.d.ts.map +1 -1
- package/dist/src/intl/zh-CN.js +1 -1
- package/dist/src/intl/zh-TW.d.ts.map +1 -1
- package/dist/src/intl/zh-TW.js +1 -1
- package/dist/src/local-first/Evolu.d.ts +12 -12
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +2 -2
- package/dist/src/local-first/Protocol.d.ts +14 -76
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +29 -206
- package/dist/src/local-first/Query.d.ts +11 -11
- package/dist/src/local-first/Query.js +6 -6
- package/dist/src/local-first/Relay.d.ts +2 -2
- package/dist/src/local-first/Schema.d.ts +3 -5
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +2 -4
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +5 -5
- package/dist/src/local-first/Storage.js +4 -4
- package/dist/src/local-first/Timestamp.d.ts +9 -0
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +13 -10
- package/package.json +7 -4
- package/src/Array.test.ts +809 -0
- package/src/Array.ts +21 -21
- package/src/Assert.test.ts +1232 -0
- package/src/Assert.ts +1023 -227
- package/src/BigInt.test.ts +28 -0
- package/src/Binary.test.ts +1128 -0
- package/src/{Buffer.ts → Binary.ts} +273 -46
- package/src/Brand.test.ts +90 -0
- package/src/Cache.test.ts +183 -0
- package/src/Callbacks.test.ts +66 -0
- package/src/Console.test.ts +809 -0
- package/src/Console.ts +9 -17
- package/src/Crypto.test.ts +138 -0
- package/src/Crypto.ts +5 -5
- package/src/Eq.test.ts +333 -0
- package/src/Eq.ts +110 -176
- package/src/Error.test.ts +118 -0
- package/src/Function.test.ts +204 -0
- package/src/Function.ts +10 -15
- package/src/Http.test.ts +816 -0
- package/src/Http.ts +6 -7
- package/src/LeakDetector.test.ts +210 -0
- package/src/LeakDetector.ts +1 -0
- package/src/LockManager.ts +2 -2
- package/src/Lookup.test.ts +418 -0
- package/src/Lookup.ts +4 -3
- package/src/Microtask.test.ts +140 -0
- package/src/Number.test.ts +218 -0
- package/src/Number.ts +2 -2
- package/src/Object.test.ts +293 -0
- package/src/Object.ts +5 -4
- package/src/Option.test.ts +104 -0
- package/src/Option.ts +1 -2
- package/src/Order.test.ts +56 -0
- package/src/Platform.ts +2 -0
- package/src/Polyfills.ts +9 -4
- package/src/Random.test.ts +89 -0
- package/src/Random.ts +1 -1
- package/src/Ref.test.ts +139 -0
- package/src/RefCount.test.ts +184 -0
- package/src/Relation.test.ts +505 -0
- package/src/Resource.test.ts +4623 -0
- package/src/Result.test.ts +1829 -0
- package/src/Result.ts +49 -54
- package/src/Schedule.test.ts +1554 -0
- package/src/Set.test.ts +279 -0
- package/src/Set.ts +6 -6
- package/src/Sqlite.ts +1 -1
- package/src/Store.test.ts +288 -0
- package/src/String.test.ts +54 -0
- package/src/String.ts +3 -3
- package/src/Task.test.ts +10820 -0
- package/src/Task.ts +38 -43
- package/src/Test.test.ts +88 -0
- package/src/Test.ts +49 -3
- package/src/Time.test.ts +926 -0
- package/src/Time.ts +13 -21
- package/src/Type.base64.test.ts +88 -0
- package/src/Type.test.ts +20465 -0
- package/src/Type.ts +114 -111
- package/src/Types.test.ts +324 -0
- package/src/Types.ts +13 -13
- package/src/WebSocket.ts +4 -7
- package/src/Worker.test.ts +755 -0
- package/src/Worker.ts +13 -18
- package/src/index.ts +1 -1
- package/src/intl/_en.ts +1 -1
- package/src/intl/ar.ts +1 -1
- package/src/intl/bn.ts +1 -1
- package/src/intl/ca.ts +1 -1
- package/src/intl/cs.ts +1 -1
- package/src/intl/da.ts +1 -1
- package/src/intl/de.ts +1 -1
- package/src/intl/el.ts +1 -1
- package/src/intl/es.ts +1 -1
- package/src/intl/fa.ts +1 -1
- package/src/intl/fi.ts +1 -1
- package/src/intl/fil.ts +1 -1
- package/src/intl/fr.ts +1 -1
- package/src/intl/he.ts +1 -1
- package/src/intl/hi.ts +1 -1
- package/src/intl/hr.ts +1 -1
- package/src/intl/hu.ts +1 -1
- package/src/intl/id.ts +1 -1
- package/src/intl/intl.test.ts +3106 -0
- package/src/intl/it.ts +1 -1
- package/src/intl/ja.ts +1 -1
- package/src/intl/ko.ts +1 -1
- package/src/intl/ml.ts +1 -1
- package/src/intl/mr.ts +1 -1
- package/src/intl/ms.ts +1 -1
- package/src/intl/nb.ts +1 -1
- package/src/intl/nl.ts +1 -1
- package/src/intl/pa.ts +1 -1
- package/src/intl/pl.ts +1 -1
- package/src/intl/pt-BR.ts +1 -1
- package/src/intl/pt.ts +1 -1
- package/src/intl/ro.ts +1 -1
- package/src/intl/sk.ts +1 -1
- package/src/intl/sl.ts +1 -1
- package/src/intl/sv.ts +1 -1
- package/src/intl/sw.ts +1 -1
- package/src/intl/ta.ts +1 -1
- package/src/intl/te.ts +1 -1
- package/src/intl/th.ts +1 -1
- package/src/intl/tr.ts +1 -1
- package/src/intl/uk.ts +1 -1
- package/src/intl/ur.ts +1 -1
- package/src/intl/vi.ts +1 -1
- package/src/intl/zh-CN.ts +1 -1
- package/src/intl/zh-TW.ts +1 -1
- package/src/local-first/Evolu.test.ts +1921 -0
- package/src/local-first/Evolu.ts +18 -14
- package/src/local-first/Owner.test.ts +83 -0
- package/src/local-first/Protocol.test.ts +1037 -0
- package/src/local-first/Protocol.ts +58 -261
- package/src/local-first/Query.test.ts +349 -0
- package/src/local-first/Query.ts +11 -11
- package/src/local-first/Relay.ts +2 -2
- package/src/local-first/Schema.ts +3 -5
- package/src/local-first/Shared.test.ts +1828 -0
- package/src/local-first/Shared.ts +9 -8
- package/src/local-first/Storage.ts +4 -4
- package/src/local-first/Timestamp.test.ts +374 -0
- package/src/local-first/Timestamp.ts +22 -10
- package/dist/src/Buffer.d.ts.map +0 -1
package/dist/src/Assert.d.ts
CHANGED
|
@@ -1,219 +1,364 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Platform-agnostic assertions for invariants, examples, and tests.
|
|
3
|
+
*
|
|
4
|
+
* An assertion documents a condition required for the program to be correct.
|
|
5
|
+
* Ideally, the type system should enforce it so incorrect code cannot compile,
|
|
6
|
+
* but that is not always possible (not even in Rust). A runtime assertion
|
|
7
|
+
* failure therefore indicates a bug that must be fixed. Throwing at the point
|
|
8
|
+
* of violation prevents invalid state from propagating and makes the defect
|
|
9
|
+
* easier to diagnose.
|
|
10
|
+
*
|
|
11
|
+
* Evolu provides its own assertions so the same concise API works for
|
|
12
|
+
* production invariants, executable examples, and tests on every platform. The
|
|
13
|
+
* general {@link assert} requires a message explaining the invariant, and
|
|
14
|
+
* specialized assertions provide focused diagnostics and narrowing. The API
|
|
15
|
+
* deliberately favors strict and predictable contracts: one {@link assertEqual}
|
|
16
|
+
* covers primitives and structural Data comparisons, {@link assertEqualBytes}
|
|
17
|
+
* compares bytes across array representations, and {@link assertSame} is
|
|
18
|
+
* reserved for SameValue or reference identity. Generic partial structural
|
|
19
|
+
* assertions such as `assertMatches` are omitted so unmentioned state cannot
|
|
20
|
+
* hide regressions. If a complete expected value is too large to keep inline,
|
|
21
|
+
* use a fixture or a focused domain helper that keeps the contract explicit.
|
|
22
|
+
* {@link assertOk} and {@link assertErr} provide convenient Result narrowing,
|
|
23
|
+
* while {@link assertType} provides runtime Type assertions and compile-time
|
|
24
|
+
* type equality similar to Vitest's `expectTypeOf` without requiring a test
|
|
25
|
+
* runner. {@link assertThrows} and {@link assertRejects} verify thrown and
|
|
26
|
+
* rejected values without matcher semantics. Together, these assertions keep
|
|
27
|
+
* documentation examples concise and directly copyable.
|
|
28
|
+
*
|
|
29
|
+
* In Node.js, failures use the native `AssertionError` for structured
|
|
30
|
+
* diagnostics and diffs. Other platforms use a compatible fallback. You should
|
|
31
|
+
* not need to import Node.js assertions unless Evolu does not provide an
|
|
32
|
+
* equivalent or a test requires exact Node.js semantics.
|
|
33
|
+
*
|
|
34
|
+
* Do not use assertions to validate external input. Use a {@link Type}
|
|
35
|
+
* declaration's `fromUnknown` so invalid input is represented by a typed
|
|
36
|
+
* {@link Result}.
|
|
37
|
+
*
|
|
38
|
+
* TODO(next major): Add a separate production build that replaces full
|
|
39
|
+
* assertion messages with numeric error codes and a decoder, following React's
|
|
40
|
+
* approach, to reduce the core bundle size since Evolu has many assertions.
|
|
3
41
|
*
|
|
4
42
|
* @module
|
|
5
43
|
*/
|
|
6
44
|
import type { NonEmptyArray, NonEmptyReadonlyArray } from "./Array.ts";
|
|
7
45
|
import { type Eq } from "./Eq.ts";
|
|
8
46
|
import type { Err, InferErr, InferOk, Ok, Result } from "./Result.ts";
|
|
9
|
-
import type {
|
|
10
|
-
import type { CompileTimeError } from "./Types.ts";
|
|
47
|
+
import type { ValueWithLength } from "./Types.ts";
|
|
11
48
|
/**
|
|
12
|
-
*
|
|
13
|
-
* not.
|
|
49
|
+
* Asserts that a condition is truthy.
|
|
14
50
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
51
|
+
* Throws an `AssertionError` with the provided message if the condition is
|
|
52
|
+
* falsy, preventing invalid state from propagating and making the failure
|
|
53
|
+
* easier to diagnose.
|
|
17
54
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
55
|
+
* Use `assert` only when a more specific assertion, such as {@link assertTrue}
|
|
56
|
+
* or {@link assertEqual}, does not apply. For that reason, `assert` requires a
|
|
57
|
+
* custom message explaining the expected condition.
|
|
58
|
+
*
|
|
59
|
+
* Options provide structured diagnostics for custom assertions. Pass an
|
|
60
|
+
* underlying failure as `cause` when it explains why the asserted condition
|
|
61
|
+
* failed, and pass the custom assertion as `stackStartFn` to omit its
|
|
62
|
+
* implementation from the stack trace.
|
|
21
63
|
*
|
|
22
64
|
* ### Example
|
|
23
65
|
*
|
|
24
66
|
* ```ts
|
|
25
|
-
* import { assert, assertEqual,
|
|
67
|
+
* import { assert, assertEqual, assertType } from "@evolu/common";
|
|
26
68
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* assertEqual(
|
|
69
|
+
* const value: unknown = "Evolu";
|
|
70
|
+
* assert(typeof value === "string", "Expected a string.");
|
|
71
|
+
*
|
|
72
|
+
* assertType<typeof value, string>();
|
|
73
|
+
* assertEqual(value, "Evolu");
|
|
32
74
|
* ```
|
|
75
|
+
*
|
|
76
|
+
* @group Assertions
|
|
33
77
|
*/
|
|
34
|
-
export declare const assert: (condition: unknown, message: string
|
|
78
|
+
export declare const assert: (condition: unknown, message: string, options?: {
|
|
79
|
+
readonly actual?: unknown;
|
|
80
|
+
readonly expected?: unknown;
|
|
81
|
+
readonly operator?: string;
|
|
82
|
+
readonly diff?: "full" | undefined;
|
|
83
|
+
readonly cause?: unknown;
|
|
84
|
+
readonly stackStartFn?: (...args: Array<never>) => unknown;
|
|
85
|
+
}) => asserts condition;
|
|
35
86
|
/**
|
|
36
|
-
* Asserts that
|
|
87
|
+
* Asserts that a value is exactly `true`.
|
|
37
88
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
89
|
+
* Boolean conditions preserve their control-flow narrowing, including named
|
|
90
|
+
* type guards. Unknown values narrow to the literal `true`. Unlike
|
|
91
|
+
* {@link assert}, this checks an exact boolean value instead of truthiness and
|
|
92
|
+
* does not require a custom message.
|
|
40
93
|
*
|
|
41
94
|
* ### Example
|
|
42
95
|
*
|
|
43
96
|
* ```ts
|
|
44
|
-
* import {
|
|
97
|
+
* import { assertTrue, assertType } from "@evolu/common";
|
|
45
98
|
*
|
|
46
99
|
* interface User {
|
|
47
100
|
* readonly name: string;
|
|
48
101
|
* }
|
|
49
102
|
*
|
|
50
|
-
* const
|
|
51
|
-
*
|
|
103
|
+
* const isUser = (value: unknown): value is User =>
|
|
104
|
+
* typeof value === "object" &&
|
|
105
|
+
* value !== null &&
|
|
106
|
+
* "name" in value &&
|
|
107
|
+
* typeof value.name === "string";
|
|
52
108
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
109
|
+
* const value: unknown = { name: "Ada" };
|
|
110
|
+
* assertTrue(isUser(value));
|
|
111
|
+
* assertType<typeof value, User>();
|
|
112
|
+
*
|
|
113
|
+
* const condition: unknown = true;
|
|
114
|
+
* assertTrue(condition);
|
|
115
|
+
* assertType<typeof condition, true>();
|
|
55
116
|
* ```
|
|
117
|
+
*
|
|
118
|
+
* @group Assertions
|
|
56
119
|
*/
|
|
57
|
-
export declare
|
|
120
|
+
export declare function assertTrue(condition: boolean): asserts condition;
|
|
121
|
+
export declare function assertTrue(value: unknown): asserts value is true;
|
|
58
122
|
/**
|
|
59
|
-
* Asserts that a value is exactly `
|
|
123
|
+
* Asserts that a value is exactly `false` and narrows it to `false`.
|
|
60
124
|
*
|
|
61
|
-
* Unlike {@link assert}, this checks an exact boolean value instead of
|
|
62
|
-
*
|
|
125
|
+
* Unlike {@link assert}, this checks an exact boolean value instead of falsiness
|
|
126
|
+
* and does not require a custom message.
|
|
63
127
|
*
|
|
64
128
|
* ### Example
|
|
65
129
|
*
|
|
66
130
|
* ```ts
|
|
67
|
-
* import {
|
|
131
|
+
* import { assertFalse, assertType } from "@evolu/common";
|
|
68
132
|
*
|
|
69
|
-
* const value: unknown =
|
|
70
|
-
*
|
|
71
|
-
* assertType<
|
|
133
|
+
* const value: unknown = false;
|
|
134
|
+
* assertFalse(value);
|
|
135
|
+
* assertType<typeof value, false>();
|
|
72
136
|
* ```
|
|
137
|
+
*
|
|
138
|
+
* @group Assertions
|
|
73
139
|
*/
|
|
74
|
-
export declare const
|
|
140
|
+
export declare const assertFalse: (value: unknown) => asserts value is false;
|
|
75
141
|
/**
|
|
76
|
-
* Asserts that a
|
|
142
|
+
* Asserts that a condition becomes true after exactly the specified number of
|
|
143
|
+
* microtasks.
|
|
77
144
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
145
|
+
* Use this in tests that intentionally specify async scheduling behavior.
|
|
146
|
+
* Application code must not depend on exact microtask counts. Maintainers
|
|
147
|
+
* should review count changes because they indicate that an async pipeline
|
|
148
|
+
* changed.
|
|
80
149
|
*
|
|
81
150
|
* ### Example
|
|
82
151
|
*
|
|
83
152
|
* ```ts
|
|
84
|
-
* import {
|
|
153
|
+
* import { assertConditionAfterMicrotasks } from "@evolu/common";
|
|
85
154
|
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
155
|
+
* let ready = false;
|
|
156
|
+
* queueMicrotask(() => {
|
|
157
|
+
* ready = true;
|
|
158
|
+
* });
|
|
159
|
+
*
|
|
160
|
+
* await assertConditionAfterMicrotasks(() => ready, 1);
|
|
89
161
|
* ```
|
|
162
|
+
*
|
|
163
|
+
* @group Assertions
|
|
90
164
|
*/
|
|
91
|
-
export declare
|
|
165
|
+
export declare function assertConditionAfterMicrotasks(condition: () => boolean, expectedMicrotaskCount: number): Promise<void>;
|
|
92
166
|
/**
|
|
93
|
-
* Asserts that
|
|
167
|
+
* Asserts that a Promise continuation runs after exactly the specified number
|
|
168
|
+
* of microtasks.
|
|
94
169
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
170
|
+
* Both fulfillment and rejection count as running the continuation. Use this in
|
|
171
|
+
* tests that intentionally specify async scheduling behavior. Application code
|
|
172
|
+
* must not depend on exact microtask counts. Maintainers should review count
|
|
173
|
+
* changes because they indicate that an async pipeline changed.
|
|
97
174
|
*
|
|
98
175
|
* ### Example
|
|
99
176
|
*
|
|
100
177
|
* ```ts
|
|
101
|
-
* import {
|
|
178
|
+
* import { assertContinuationAfterMicrotasks } from "@evolu/common";
|
|
102
179
|
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
180
|
+
* await assertContinuationAfterMicrotasks(Promise.resolve("ready"), 1);
|
|
181
|
+
* ```
|
|
105
182
|
*
|
|
106
|
-
*
|
|
183
|
+
* @group Assertions
|
|
184
|
+
*/
|
|
185
|
+
export declare const assertContinuationAfterMicrotasks: (promise: Promise<unknown>, expectedMicrotaskCount: number) => Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Asserts that a function throws the expected value.
|
|
188
|
+
*
|
|
189
|
+
* Expected values use {@link assertEqual} semantics. To perform several or
|
|
190
|
+
* specialized assertions, pass an assertion function that receives the thrown
|
|
191
|
+
* value and returns nothing. Returning a predicate result fails. Use
|
|
192
|
+
* {@link assertThrowsSame} for SameValue or reference identity and
|
|
193
|
+
* {@link assertThrowsInstanceOf} for a runtime type.
|
|
194
|
+
*
|
|
195
|
+
* Because JavaScript permits throwing functions, a function second argument is
|
|
196
|
+
* always treated as an assertion function. Use `assertThrowsSame` to assert
|
|
197
|
+
* that a particular function was thrown.
|
|
198
|
+
*
|
|
199
|
+
* ### Example
|
|
200
|
+
*
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { assertThrows } from "@evolu/common";
|
|
203
|
+
*
|
|
204
|
+
* assertThrows(
|
|
205
|
+
* () => {
|
|
206
|
+
* // oxlint-disable-next-line eslint/no-throw-literal, typescript/only-throw-error -- Exercise arbitrary thrown values.
|
|
207
|
+
* throw { type: "ExpectedFailure", value: 42 };
|
|
208
|
+
* },
|
|
209
|
+
* { type: "ExpectedFailure", value: 42 },
|
|
210
|
+
* );
|
|
107
211
|
* ```
|
|
212
|
+
*
|
|
213
|
+
* @group Assertions
|
|
108
214
|
*/
|
|
109
|
-
export declare function
|
|
110
|
-
|
|
111
|
-
error: CompileTimeError<"assertEqual", "Actual and expected values must consist only of Data.">
|
|
112
|
-
];
|
|
215
|
+
export declare function assertThrows(run: () => unknown, assertThrown: (thrown: unknown) => void): void;
|
|
216
|
+
export declare function assertThrows(run: () => unknown, expected: unknown): void;
|
|
113
217
|
/**
|
|
114
|
-
* Asserts that a
|
|
115
|
-
* and narrows the Result.
|
|
218
|
+
* Asserts that a function throws the same value using `Object.is`.
|
|
116
219
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* inferred independently, so an unbranded literal can compare a branded
|
|
120
|
-
* primitive or collection. Pass a custom {@link Eq} when either value is outside
|
|
121
|
-
* {@link Data} or needs domain-specific equality.
|
|
220
|
+
* Use this to verify that a thrown value was propagated unchanged. For value
|
|
221
|
+
* comparisons, use {@link assertThrows}.
|
|
122
222
|
*
|
|
123
223
|
* ### Example
|
|
124
224
|
*
|
|
125
225
|
* ```ts
|
|
126
|
-
* import {
|
|
127
|
-
* assertEqual,
|
|
128
|
-
* assertOk,
|
|
129
|
-
* assertType,
|
|
130
|
-
* ok,
|
|
131
|
-
* type Ok,
|
|
132
|
-
* type Result,
|
|
133
|
-
* type Typed,
|
|
134
|
-
* } from "@evolu/common";
|
|
226
|
+
* import { assertThrowsSame } from "@evolu/common";
|
|
135
227
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
228
|
+
* const expected = new Error("Unavailable.");
|
|
229
|
+
* assertThrowsSame(() => {
|
|
230
|
+
* throw expected;
|
|
231
|
+
* }, expected);
|
|
232
|
+
* ```
|
|
139
233
|
*
|
|
140
|
-
*
|
|
234
|
+
* @group Assertions
|
|
235
|
+
*/
|
|
236
|
+
export declare const assertThrowsSame: (run: () => unknown, expected: unknown) => void;
|
|
237
|
+
/**
|
|
238
|
+
* Asserts that a function throws an instance of a constructor.
|
|
141
239
|
*
|
|
142
|
-
*
|
|
240
|
+
* Returns the narrowed instance so additional properties can be asserted
|
|
241
|
+
* without running the function again.
|
|
242
|
+
*
|
|
243
|
+
* ### Example
|
|
244
|
+
*
|
|
245
|
+
* ```ts
|
|
246
|
+
* import { assertEqual, assertThrowsInstanceOf } from "@evolu/common";
|
|
247
|
+
*
|
|
248
|
+
* const error = assertThrowsInstanceOf(() => {
|
|
249
|
+
* throw new TypeError("Expected a string.");
|
|
250
|
+
* }, TypeError);
|
|
251
|
+
* assertEqual(error.message, "Expected a string.");
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* @group Assertions
|
|
255
|
+
*/
|
|
256
|
+
export declare const assertThrowsInstanceOf: <T extends {
|
|
257
|
+
readonly name: string;
|
|
258
|
+
} & (abstract new (...args: Array<never>) => unknown)>(run: () => unknown, constructor: T) => InstanceType<T>;
|
|
259
|
+
/**
|
|
260
|
+
* Asserts that a promise rejects with the expected value.
|
|
261
|
+
*
|
|
262
|
+
* Expected values use {@link assertEqual} semantics. To perform several or
|
|
263
|
+
* specialized assertions, pass an assertion function that receives the
|
|
264
|
+
* rejection reason and returns nothing. Returning a predicate result fails. Use
|
|
265
|
+
* {@link assertRejectsSame} for SameValue or reference identity and
|
|
266
|
+
* {@link assertRejectsInstanceOf} for a runtime type.
|
|
143
267
|
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
268
|
+
* Because JavaScript permits rejecting with functions, a function second
|
|
269
|
+
* argument is always treated as an assertion function. Use `assertRejectsSame`
|
|
270
|
+
* to assert that a particular function was rejected.
|
|
271
|
+
*
|
|
272
|
+
* ### Example
|
|
273
|
+
*
|
|
274
|
+
* ```ts
|
|
275
|
+
* import { assertRejects } from "@evolu/common";
|
|
276
|
+
*
|
|
277
|
+
* const expected = new Error("Unavailable.");
|
|
278
|
+
* await assertRejects(Promise.reject(expected), expected);
|
|
147
279
|
* ```
|
|
280
|
+
*
|
|
281
|
+
* @group Assertions
|
|
148
282
|
*/
|
|
149
|
-
export declare function
|
|
150
|
-
export declare function
|
|
151
|
-
export declare function assertOk<R extends Result<unknown, unknown>, ExpectedValue>(result: R, ...comparison: [expectedValue: ExpectedValue] & (IsData<InferOk<R> | ExpectedValue> extends true ? unknown : {
|
|
152
|
-
readonly [Error in CompileTimeError<"assertOk", "Result value and expected value must consist only of Data when no custom Eq is provided.">]: never;
|
|
153
|
-
})): asserts result is Extract<R, Ok<unknown>>;
|
|
283
|
+
export declare function assertRejects(promise: PromiseLike<unknown>, assertRejected: (reason: unknown) => void): Promise<void>;
|
|
284
|
+
export declare function assertRejects(promise: PromiseLike<unknown>, expected: unknown): Promise<void>;
|
|
154
285
|
/**
|
|
155
|
-
* Asserts that a
|
|
156
|
-
* and narrows the Result.
|
|
286
|
+
* Asserts that a promise rejects with the same value using `Object.is`.
|
|
157
287
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* inferred independently, so an unbranded literal can compare a branded
|
|
161
|
-
* primitive or collection. Pass a custom {@link Eq} when either value is outside
|
|
162
|
-
* {@link Data} or needs domain-specific equality.
|
|
288
|
+
* Use this to verify that a rejection reason was propagated unchanged. For
|
|
289
|
+
* value comparisons, use {@link assertRejects}.
|
|
163
290
|
*
|
|
164
291
|
* ### Example
|
|
165
292
|
*
|
|
166
293
|
* ```ts
|
|
167
|
-
* import {
|
|
168
|
-
* assertEqual,
|
|
169
|
-
* assertErr,
|
|
170
|
-
* assertType,
|
|
171
|
-
* err,
|
|
172
|
-
* type Err,
|
|
173
|
-
* type Result,
|
|
174
|
-
* type Typed,
|
|
175
|
-
* } from "@evolu/common";
|
|
294
|
+
* import { assertRejectsSame } from "@evolu/common";
|
|
176
295
|
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
296
|
+
* const expected = new Error("Unavailable.");
|
|
297
|
+
* await assertRejectsSame(Promise.reject(expected), expected);
|
|
298
|
+
* ```
|
|
180
299
|
*
|
|
181
|
-
*
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
300
|
+
* @group Assertions
|
|
301
|
+
*/
|
|
302
|
+
export declare const assertRejectsSame: (promise: PromiseLike<unknown>, expected: unknown) => Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Asserts that a promise rejects with an instance of a constructor.
|
|
185
305
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
306
|
+
* Returns the narrowed instance so additional properties can be asserted.
|
|
307
|
+
*
|
|
308
|
+
* ### Example
|
|
309
|
+
*
|
|
310
|
+
* ```ts
|
|
311
|
+
* import { assertEqual, assertRejectsInstanceOf } from "@evolu/common";
|
|
312
|
+
*
|
|
313
|
+
* const error = await assertRejectsInstanceOf(
|
|
314
|
+
* Promise.reject(new TypeError("Unavailable.")),
|
|
315
|
+
* TypeError,
|
|
316
|
+
* );
|
|
317
|
+
* assertEqual(error.message, "Unavailable.");
|
|
189
318
|
* ```
|
|
319
|
+
*
|
|
320
|
+
* @group Assertions
|
|
190
321
|
*/
|
|
191
|
-
export declare
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
322
|
+
export declare const assertRejectsInstanceOf: <T extends {
|
|
323
|
+
readonly name: string;
|
|
324
|
+
} & (abstract new (...args: Array<never>) => unknown)>(promise: PromiseLike<unknown>, constructor: T) => Promise<InstanceType<T>>;
|
|
325
|
+
/**
|
|
326
|
+
* Asserts that a value is an instance of a constructor and narrows it.
|
|
327
|
+
*
|
|
328
|
+
* ### Example
|
|
329
|
+
*
|
|
330
|
+
* ```ts
|
|
331
|
+
* import { assertInstanceOf, assertType } from "@evolu/common";
|
|
332
|
+
*
|
|
333
|
+
* const value: unknown = new TypeError("Invalid value.");
|
|
334
|
+
* assertInstanceOf(value, TypeError);
|
|
335
|
+
* assertType<typeof value, TypeError>();
|
|
336
|
+
* ```
|
|
337
|
+
*
|
|
338
|
+
* @group Assertions
|
|
339
|
+
*/
|
|
340
|
+
export declare const assertInstanceOf: <Constructor extends {
|
|
341
|
+
readonly name: string;
|
|
342
|
+
} & (abstract new (...args: Array<never>) => unknown)>(value: unknown, constructor: Constructor) => asserts value is InstanceType<Constructor>;
|
|
196
343
|
/**
|
|
197
344
|
* Asserts that a value is non-nullable.
|
|
198
345
|
*
|
|
199
346
|
* Following TypeScript's {@link NonNullable}, non-nullable here means neither
|
|
200
|
-
* null nor undefined.
|
|
201
|
-
*
|
|
347
|
+
* null nor undefined.
|
|
348
|
+
*
|
|
349
|
+
* @group Assertions
|
|
202
350
|
*/
|
|
203
351
|
export declare const assertNonNullable: <T>(value: T, message?: string) => asserts value is NonNullable<T>;
|
|
204
352
|
/**
|
|
205
353
|
* Asserts that a value is not null while preserving undefined.
|
|
206
354
|
*
|
|
207
|
-
* Use this when a value is logically guaranteed not to be null but TypeScript
|
|
208
|
-
* cannot prove it.
|
|
209
|
-
*
|
|
210
355
|
* ### Example
|
|
211
356
|
*
|
|
212
357
|
* ```ts
|
|
213
358
|
* import {
|
|
214
|
-
* assert,
|
|
215
359
|
* assertEqual,
|
|
216
360
|
* assertErr,
|
|
361
|
+
* assertInstanceOf,
|
|
217
362
|
* assertNotNull,
|
|
218
363
|
* assertType,
|
|
219
364
|
* trySync,
|
|
@@ -221,28 +366,29 @@ export declare const assertNonNullable: <T>(value: T, message?: string) => asser
|
|
|
221
366
|
*
|
|
222
367
|
* const value = undefined as string | null | undefined;
|
|
223
368
|
* assertNotNull(value);
|
|
224
|
-
*
|
|
369
|
+
*
|
|
370
|
+
* assertType<typeof value, string | undefined>();
|
|
225
371
|
* assertEqual(value, undefined);
|
|
372
|
+
*
|
|
226
373
|
* const result = trySync(() => assertNotNull(null));
|
|
227
374
|
* assertErr(result);
|
|
228
|
-
*
|
|
375
|
+
* assertInstanceOf(result.error, Error);
|
|
229
376
|
* assertEqual(result.error.message, "Expected value not to be null.");
|
|
230
377
|
* ```
|
|
378
|
+
*
|
|
379
|
+
* @group Assertions
|
|
231
380
|
*/
|
|
232
381
|
export declare const assertNotNull: <T>(value: T, message?: string) => asserts value is T & ({} | undefined);
|
|
233
382
|
/**
|
|
234
383
|
* Asserts that a value is not undefined while preserving null.
|
|
235
384
|
*
|
|
236
|
-
* Use this when a value is logically guaranteed not to be undefined but
|
|
237
|
-
* TypeScript cannot prove it.
|
|
238
|
-
*
|
|
239
385
|
* ### Example
|
|
240
386
|
*
|
|
241
387
|
* ```ts
|
|
242
388
|
* import {
|
|
243
|
-
* assert,
|
|
244
389
|
* assertEqual,
|
|
245
390
|
* assertErr,
|
|
391
|
+
* assertInstanceOf,
|
|
246
392
|
* assertNotUndefined,
|
|
247
393
|
* assertType,
|
|
248
394
|
* trySync,
|
|
@@ -250,20 +396,37 @@ export declare const assertNotNull: <T>(value: T, message?: string) => asserts v
|
|
|
250
396
|
*
|
|
251
397
|
* const value = null as string | null | undefined;
|
|
252
398
|
* assertNotUndefined(value);
|
|
253
|
-
* assertType<string | null
|
|
399
|
+
* assertType<typeof value, string | null>();
|
|
254
400
|
* assertEqual(value, null);
|
|
255
401
|
* const result = trySync(() => assertNotUndefined(undefined));
|
|
256
402
|
* assertErr(result);
|
|
257
|
-
*
|
|
403
|
+
* assertInstanceOf(result.error, Error);
|
|
258
404
|
* assertEqual(result.error.message, "Expected value not to be undefined.");
|
|
259
405
|
* ```
|
|
406
|
+
*
|
|
407
|
+
* @group Assertions
|
|
260
408
|
*/
|
|
261
409
|
export declare const assertNotUndefined: <T>(value: T, message?: string) => asserts value is T & ({} | null);
|
|
262
410
|
/**
|
|
263
|
-
* Asserts that
|
|
411
|
+
* Asserts that a value has the expected length and narrows its length.
|
|
264
412
|
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
413
|
+
* ### Example
|
|
414
|
+
*
|
|
415
|
+
* ```ts
|
|
416
|
+
* import { assertLength, assertType } from "@evolu/common";
|
|
417
|
+
*
|
|
418
|
+
* const values: ReadonlyArray<string> = ["Ada", "Grace"];
|
|
419
|
+
* assertLength(values, 2);
|
|
420
|
+
* assertType<typeof values.length, 2>();
|
|
421
|
+
* ```
|
|
422
|
+
*
|
|
423
|
+
* @group Assertions
|
|
424
|
+
*/
|
|
425
|
+
export declare const assertLength: <Value extends ValueWithLength, const Length extends number>(value: Value, expectedLength: Length) => asserts value is Value & {
|
|
426
|
+
readonly length: Length;
|
|
427
|
+
};
|
|
428
|
+
/**
|
|
429
|
+
* Asserts that an array is non-empty.
|
|
267
430
|
*
|
|
268
431
|
* ### Example
|
|
269
432
|
*
|
|
@@ -279,19 +442,18 @@ export declare const assertNotUndefined: <T>(value: T, message?: string) => asse
|
|
|
279
442
|
*
|
|
280
443
|
* const values = [1, 2, 3];
|
|
281
444
|
* assertNonEmptyArray(values);
|
|
282
|
-
* assertType<NonEmptyArray<number
|
|
445
|
+
* assertType<typeof values, NonEmptyArray<number>>();
|
|
283
446
|
* assertEqual(values[0], 1);
|
|
284
447
|
* const result = trySync(() => assertNonEmptyArray([]));
|
|
285
448
|
* assertErr(result);
|
|
286
449
|
* ```
|
|
450
|
+
*
|
|
451
|
+
* @group Assertions
|
|
287
452
|
*/
|
|
288
453
|
export declare const assertNonEmptyArray: <T>(arr: Array<T>, message?: string) => asserts arr is NonEmptyArray<T>;
|
|
289
454
|
/**
|
|
290
455
|
* Asserts that a readonly array is non-empty.
|
|
291
456
|
*
|
|
292
|
-
* Use this when a readonly array is logically guaranteed to be non-empty but
|
|
293
|
-
* TypeScript cannot prove it.
|
|
294
|
-
*
|
|
295
457
|
* ### Example
|
|
296
458
|
*
|
|
297
459
|
* ```ts
|
|
@@ -306,11 +468,13 @@ export declare const assertNonEmptyArray: <T>(arr: Array<T>, message?: string) =
|
|
|
306
468
|
*
|
|
307
469
|
* const values: ReadonlyArray<number> = [1, 2, 3];
|
|
308
470
|
* assertNonEmptyReadonlyArray(values);
|
|
309
|
-
* assertType<NonEmptyReadonlyArray<number
|
|
471
|
+
* assertType<typeof values, NonEmptyReadonlyArray<number>>();
|
|
310
472
|
* assertEqual(values[0], 1);
|
|
311
473
|
* const result = trySync(() => assertNonEmptyReadonlyArray([]));
|
|
312
474
|
* assertErr(result);
|
|
313
475
|
* ```
|
|
476
|
+
*
|
|
477
|
+
* @group Assertions
|
|
314
478
|
*/
|
|
315
479
|
export declare const assertNonEmptyReadonlyArray: <T>(arr: ReadonlyArray<T>, message?: string) => asserts arr is NonEmptyReadonlyArray<T>;
|
|
316
480
|
/**
|
|
@@ -322,29 +486,222 @@ export declare const assertNonEmptyReadonlyArray: <T>(arr: ReadonlyArray<T>, mes
|
|
|
322
486
|
*
|
|
323
487
|
* ```ts
|
|
324
488
|
* import {
|
|
325
|
-
* assert,
|
|
326
489
|
* assertEqual,
|
|
327
490
|
* assertErr,
|
|
491
|
+
* assertInstanceOf,
|
|
328
492
|
* assertNotDisposed,
|
|
329
493
|
* trySync,
|
|
330
494
|
* } from "@evolu/common";
|
|
331
495
|
*
|
|
332
|
-
* await using disposer = new
|
|
496
|
+
* await using disposer = new AsyncDisposableStack();
|
|
333
497
|
* assertNotDisposed(disposer);
|
|
334
498
|
* await disposer.disposeAsync();
|
|
335
499
|
* const result = trySync(() => assertNotDisposed(disposer));
|
|
336
500
|
* assertErr(result);
|
|
337
|
-
*
|
|
501
|
+
* assertInstanceOf(result.error, Error);
|
|
338
502
|
* assertEqual(result.error.message, "Cannot use a disposed object.");
|
|
339
503
|
* ```
|
|
340
504
|
*
|
|
341
505
|
* This is the JavaScript equivalent of the .NET `ObjectDisposedException`
|
|
342
506
|
* pattern: once a helper has been disposed, calling its synchronous methods is
|
|
343
|
-
* a
|
|
344
|
-
*
|
|
507
|
+
* a bug and should throw immediately instead of continuing with invalid state.
|
|
508
|
+
*
|
|
509
|
+
* @group Assertions
|
|
345
510
|
*/
|
|
346
511
|
export declare const assertNotDisposed: (value: DisposableStack | AsyncDisposableStack | {
|
|
347
512
|
readonly disposed: boolean;
|
|
348
513
|
}) => void;
|
|
349
|
-
|
|
514
|
+
/**
|
|
515
|
+
* Asserts that two values are the same using `Object.is`.
|
|
516
|
+
*
|
|
517
|
+
* Use this when exact sameness is the contract, such as asserting reference
|
|
518
|
+
* identity or narrowing the actual value to the expected value's type, as shown
|
|
519
|
+
* below. For value comparisons, use {@link assertEqual}.
|
|
520
|
+
*
|
|
521
|
+
* Uses the same equality semantics as `assert.strictEqual` from
|
|
522
|
+
* `node:assert/strict`, but is platform-agnostic. See {@link eqSameValue} for
|
|
523
|
+
* the equality semantics.
|
|
524
|
+
*
|
|
525
|
+
* ### Example
|
|
526
|
+
*
|
|
527
|
+
* ```ts
|
|
528
|
+
* import { assertSame, assertType } from "@evolu/common";
|
|
529
|
+
*
|
|
530
|
+
* interface User {
|
|
531
|
+
* readonly name: string;
|
|
532
|
+
* }
|
|
533
|
+
*
|
|
534
|
+
* const user: User = { name: "Ada" };
|
|
535
|
+
* const value: unknown = user;
|
|
536
|
+
*
|
|
537
|
+
* assertSame(value, user);
|
|
538
|
+
* // `assertSame` narrows `value` from `unknown` to `User`.
|
|
539
|
+
* assertType<typeof value, User>();
|
|
540
|
+
* ```
|
|
541
|
+
*
|
|
542
|
+
* @group Assertions
|
|
543
|
+
*/
|
|
544
|
+
export declare const assertSame: <Expected>(actual: unknown, expected: Expected) => asserts actual is Expected;
|
|
545
|
+
/**
|
|
546
|
+
* Asserts that two values are not the same using `Object.is`.
|
|
547
|
+
*
|
|
548
|
+
* The opposite of {@link assertSame}.
|
|
549
|
+
*
|
|
550
|
+
* ### Example
|
|
551
|
+
*
|
|
552
|
+
* ```ts
|
|
553
|
+
* import { assertNotSame } from "@evolu/common";
|
|
554
|
+
*
|
|
555
|
+
* const first = { name: "Ada" };
|
|
556
|
+
* const second = { name: "Ada" };
|
|
557
|
+
*
|
|
558
|
+
* assertNotSame(first, second);
|
|
559
|
+
* assertNotSame(0, -0);
|
|
560
|
+
* ```
|
|
561
|
+
*
|
|
562
|
+
* @group Assertions
|
|
563
|
+
*/
|
|
564
|
+
export declare const assertNotSame: (actual: unknown, expected: unknown) => void;
|
|
565
|
+
/**
|
|
566
|
+
* Asserts that two values are equal.
|
|
567
|
+
*
|
|
568
|
+
* Use this for value comparisons of both primitive and deeply structured
|
|
569
|
+
* values. Use {@link assertSame} when exact sameness or narrowing is required.
|
|
570
|
+
*
|
|
571
|
+
* Like `assert.deepEqual` from `node:assert/strict`, it performs deep
|
|
572
|
+
* structural comparison, but uses deliberately smaller, platform-agnostic
|
|
573
|
+
* equality logic based on {@link eqData}. More complex comparison logic is
|
|
574
|
+
* difficult to reason about and rarely needed. Other values are opaque and
|
|
575
|
+
* compare equal only by identity. If broader comparison semantics are needed in
|
|
576
|
+
* Node.js, use the native assertion instead.
|
|
577
|
+
*
|
|
578
|
+
* ### Example
|
|
579
|
+
*
|
|
580
|
+
* ```ts
|
|
581
|
+
* import { assertEqual } from "@evolu/common";
|
|
582
|
+
*
|
|
583
|
+
* assertEqual(21 * 2, 42);
|
|
584
|
+
*
|
|
585
|
+
* const actual: unknown = new Map([
|
|
586
|
+
* ["roles", new Set(["admin", "author"])],
|
|
587
|
+
* ]);
|
|
588
|
+
* const expected = new Map([["roles", new Set(["author", "admin"])]]);
|
|
589
|
+
*
|
|
590
|
+
* assertEqual(actual, expected);
|
|
591
|
+
* ```
|
|
592
|
+
*
|
|
593
|
+
* @group Assertions
|
|
594
|
+
*/
|
|
595
|
+
export declare function assertEqual(actual: unknown, expected: unknown): void;
|
|
596
|
+
/**
|
|
597
|
+
* Asserts that a `Uint8Array` contains the expected bytes.
|
|
598
|
+
*
|
|
599
|
+
* Uses {@link eqArrayNumber}, so the runtime representations may differ. For
|
|
600
|
+
* example, a `Uint8Array` can be compared directly with a regular array.
|
|
601
|
+
*
|
|
602
|
+
* ### Example
|
|
603
|
+
*
|
|
604
|
+
* ```ts
|
|
605
|
+
* import { assertEqualBytes } from "@evolu/common";
|
|
606
|
+
*
|
|
607
|
+
* const bytes = new Uint8Array([1, 5, 39, 254]);
|
|
608
|
+
*
|
|
609
|
+
* assertEqualBytes(bytes, [1, 5, 39, 254]);
|
|
610
|
+
* ```
|
|
611
|
+
*
|
|
612
|
+
* @group Assertions
|
|
613
|
+
*/
|
|
614
|
+
export declare const assertEqualBytes: (actual: Uint8Array, expected: ArrayLike<number>) => void;
|
|
615
|
+
/**
|
|
616
|
+
* Asserts that two values are not equal.
|
|
617
|
+
*
|
|
618
|
+
* The opposite of {@link assertEqual}.
|
|
619
|
+
*
|
|
620
|
+
* ### Example
|
|
621
|
+
*
|
|
622
|
+
* ```ts
|
|
623
|
+
* import { assertNotEqual } from "@evolu/common";
|
|
624
|
+
*
|
|
625
|
+
* assertNotEqual({ name: "Ada" }, { name: "Grace" });
|
|
626
|
+
* assertNotEqual([1, 2], [2, 1]);
|
|
627
|
+
* ```
|
|
628
|
+
*
|
|
629
|
+
* @group Assertions
|
|
630
|
+
*/
|
|
631
|
+
export declare function assertNotEqual(actual: unknown, expected: unknown): void;
|
|
632
|
+
/**
|
|
633
|
+
* Asserts that a {@link Result} is an {@link Ok} and narrows it, optionally
|
|
634
|
+
* comparing its value.
|
|
635
|
+
*
|
|
636
|
+
* When an expected value is provided, it is compared using {@link assertEqual}
|
|
637
|
+
* semantics by default. Pass a custom {@link Eq} for different equality
|
|
638
|
+
* semantics.
|
|
639
|
+
*
|
|
640
|
+
* ### Example
|
|
641
|
+
*
|
|
642
|
+
* ```ts
|
|
643
|
+
* import {
|
|
644
|
+
* assertOk,
|
|
645
|
+
* assertType,
|
|
646
|
+
* ok,
|
|
647
|
+
* type Ok,
|
|
648
|
+
* type Result,
|
|
649
|
+
* type Typed,
|
|
650
|
+
* } from "@evolu/common";
|
|
651
|
+
*
|
|
652
|
+
* interface User {
|
|
653
|
+
* readonly id: string;
|
|
654
|
+
* }
|
|
655
|
+
*
|
|
656
|
+
* interface UserNotFoundError extends Typed<"UserNotFound"> {}
|
|
657
|
+
*
|
|
658
|
+
* const result: Result<User, UserNotFoundError> = ok({ id: "user-1" });
|
|
659
|
+
*
|
|
660
|
+
* assertOk(result, { id: "user-1" });
|
|
661
|
+
* assertType<typeof result, Ok<User>>();
|
|
662
|
+
* ```
|
|
663
|
+
*
|
|
664
|
+
* @group Assertions
|
|
665
|
+
*/
|
|
666
|
+
export declare function assertOk<R extends Result<unknown, unknown>>(result: R): asserts result is Extract<R, Ok<unknown>>;
|
|
667
|
+
export declare function assertOk<R extends Result<unknown, unknown>>(result: R, expectedValue: InferOk<R>, eq: Eq<InferOk<R>>): asserts result is Extract<R, Ok<unknown>>;
|
|
668
|
+
export declare function assertOk<R extends Result<unknown, unknown>>(result: R, expectedValue: unknown): asserts result is Extract<R, Ok<unknown>>;
|
|
669
|
+
/**
|
|
670
|
+
* Asserts that a {@link Result} is an {@link Err} and narrows it, optionally
|
|
671
|
+
* comparing its error.
|
|
672
|
+
*
|
|
673
|
+
* When an expected error is provided, it is compared using {@link assertEqual}
|
|
674
|
+
* semantics by default. Pass a custom {@link Eq} for different equality
|
|
675
|
+
* semantics.
|
|
676
|
+
*
|
|
677
|
+
* ### Example
|
|
678
|
+
*
|
|
679
|
+
* ```ts
|
|
680
|
+
* import {
|
|
681
|
+
* assertErr,
|
|
682
|
+
* assertType,
|
|
683
|
+
* err,
|
|
684
|
+
* type Err,
|
|
685
|
+
* type Result,
|
|
686
|
+
* type Typed,
|
|
687
|
+
* } from "@evolu/common";
|
|
688
|
+
*
|
|
689
|
+
* interface UserNotFoundError extends Typed<"UserNotFound"> {
|
|
690
|
+
* readonly id: string;
|
|
691
|
+
* }
|
|
692
|
+
*
|
|
693
|
+
* const result: Result<string, UserNotFoundError> = err({
|
|
694
|
+
* type: "UserNotFound",
|
|
695
|
+
* id: "user-1",
|
|
696
|
+
* });
|
|
697
|
+
*
|
|
698
|
+
* assertErr(result, { type: "UserNotFound", id: "user-1" });
|
|
699
|
+
* assertType<typeof result, Err<UserNotFoundError>>();
|
|
700
|
+
* ```
|
|
701
|
+
*
|
|
702
|
+
* @group Assertions
|
|
703
|
+
*/
|
|
704
|
+
export declare function assertErr<R extends Result<unknown, unknown>>(result: R): asserts result is Extract<R, Err<unknown>>;
|
|
705
|
+
export declare function assertErr<R extends Result<unknown, unknown>>(result: R, expectedError: InferErr<R>, eq: Eq<InferErr<R>>): asserts result is Extract<R, Err<unknown>>;
|
|
706
|
+
export declare function assertErr<R extends Result<unknown, unknown>>(result: R, expectedError: unknown): asserts result is Extract<R, Err<unknown>>;
|
|
350
707
|
//# sourceMappingURL=Assert.d.ts.map
|