1
1
import type { Predicate } from "./type.ts" ;
2
2
3
- /**
4
- * A factory function that generates assertion error messages.
5
- */
6
- export type AssertMessageFactory = (
7
- x : unknown ,
8
- pred : Predicate < unknown > ,
9
- name ?: string ,
10
- ) => string ;
11
-
12
- /**
13
- * The default factory function used to generate assertion error messages.
14
- */
15
- export const defaultAssertMessageFactory : AssertMessageFactory = (
16
- x ,
17
- pred ,
18
- name ,
19
- ) => {
3
+ const assertMessageFactory = ( x : unknown , pred : Predicate < unknown > ) => {
20
4
const p = pred . name || "anonymous predicate" ;
21
5
const t = typeof x ;
22
6
const v = JSON . stringify ( x , null , 2 ) ;
23
- return `Expected ${
24
- name ?? "a value"
25
- } that satisfies the predicate ${ p } , got ${ t } : ${ v } `;
7
+ return `Expected a value that satisfies the predicate ${ p } , got ${ t } : ${ v } ` ;
26
8
} ;
27
9
28
- let assertMessageFactory = defaultAssertMessageFactory ;
29
-
30
10
/**
31
11
* Represents an error that occurs when an assertion fails.
32
12
*/
33
13
export class AssertError extends Error {
34
14
/**
35
- * Constructs a new instance.
36
- * @param message The error message.
15
+ * The value that failed the assertion.
37
16
*/
38
- constructor ( message ?: string ) {
39
- super ( message ) ;
17
+ readonly x : unknown ;
40
18
41
- if ( Error . captureStackTrace ) {
42
- Error . captureStackTrace ( this , AssertError ) ;
43
- }
19
+ /**
20
+ * The predicate that the value failed to satisfy.
21
+ */
22
+ readonly pred : Predicate < unknown > ;
44
23
24
+ /**
25
+ * Constructs a new instance.
26
+ *
27
+ * @param x The value that failed the assertion.
28
+ * @param pred The predicate that the value failed to satisfy.
29
+ */
30
+ constructor ( x : unknown , pred : Predicate < unknown > ) {
31
+ super ( assertMessageFactory ( x , pred ) ) ;
45
32
this . name = this . constructor . name ;
33
+ this . x = x ;
34
+ this . pred = pred ;
46
35
}
47
36
}
48
37
49
- /**
50
- * Sets the factory function used to generate assertion error messages.
51
- *
52
- * ```ts
53
- * import { is, setAssertMessageFactory } from "@core/unknownutil";
54
- *
55
- * setAssertMessageFactory((x, pred) => {
56
- * if (pred === is.String) {
57
- * return `Expected a string, got ${typeof x}`;
58
- * } else if (pred === is.Number) {
59
- * return `Expected a number, got ${typeof x}`;
60
- * } else if (pred === is.Boolean) {
61
- * return `Expected a boolean, got ${typeof x}`;
62
- * } else {
63
- * return `Expected a value that satisfies the predicate, got ${typeof x}`;
64
- * }
65
- * });
66
- * ```
67
- *
68
- * @param factory The factory function.
69
- */
70
- export function setAssertMessageFactory ( factory : AssertMessageFactory ) : void {
71
- assertMessageFactory = factory ;
72
- }
73
-
74
38
/**
75
39
* Asserts that the given value satisfies the provided predicate.
76
40
*
77
41
* It throws {@linkcode AssertError} if the value does not satisfy the predicate.
78
42
*
43
+ * @param x The value to be asserted.
44
+ * @param pred The predicate function to test the value against.
45
+ * @returns The function has a return type of `asserts x is T` to help TypeScript narrow down the type of `x` after the assertion.
46
+ *
79
47
* ```ts
80
48
* import { assert, is } from "@core/unknownutil";
81
49
*
@@ -84,19 +52,22 @@ export function setAssertMessageFactory(factory: AssertMessageFactory): void {
84
52
* // a is now narrowed to string
85
53
* ```
86
54
*
87
- * @param x The value to be asserted.
88
- * @param pred The predicate function to test the value against.
89
- * @param options Optional configuration for the assertion.
90
- * @returns The function has a return type of `asserts x is T` to help TypeScript narrow down the type of `x` after the assertion.
55
+ * Use {@linkcode https://jsr.io/@core/errorutil/doc/alter/~/alter|@core/errorutil/alter.alter} to alter error.
56
+ *
57
+ * ```ts
58
+ * import { alter } from "@core/errorutil/alter";
59
+ * import { assert, is } from "@core/unknownutil";
60
+ *
61
+ * const a: unknown = 42;
62
+ * alter(() => assert(a, is.String), new Error("a is not a string"));
63
+ * // Error: a is not a string
64
+ * ```
91
65
*/
92
66
export function assert < T > (
93
67
x : unknown ,
94
68
pred : Predicate < T > ,
95
- options : { message ?: string ; name ?: string } = { } ,
96
69
) : asserts x is T {
97
70
if ( ! pred ( x ) ) {
98
- throw new AssertError (
99
- options . message ?? assertMessageFactory ( x , pred , options . name ) ,
100
- ) ;
71
+ throw new AssertError ( x , pred ) ;
101
72
}
102
73
}
0 commit comments