Skip to content

Commit 3c7c494

Browse files
committed
feat!(ensure): remove options from ensure
Now `ensure` does not accept options. Use `@core/errorutil/alter` to throw a custom error.
1 parent 6f1acaa commit 3c7c494

File tree

4 files changed

+32
-49
lines changed

4 files changed

+32
-49
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,22 @@ const a: unknown = "Hello";
211211

212212
// `ensure` returns `string` or throws an `AssertError`
213213
const _: string = ensure(a, is.String);
214+
```
214215

215-
// With custom message
216-
const __: string = ensure(a, is.String, { message: "a must be a string" });
216+
Use [`@core/errorutil/alter`](https://jsr.io/@core/errorutil/doc/alter/~/alter)
217+
to throw a custom error:
218+
219+
```typescript
220+
import { alter } from "@core/errorutil/alter";
221+
import { ensure, is } from "@core/unknownutil";
222+
223+
const a: unknown = 0;
224+
225+
// The following throws an Error("a is not a string")
226+
const _: string = alter(
227+
() => ensure(a, is.String),
228+
new Error("a is not a string"),
229+
);
217230
```
218231

219232
### maybe

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
]
7070
},
7171
"imports": {
72+
"@core/errorutil": "jsr:@core/errorutil@^1.2.0",
7273
"@core/iterutil": "jsr:@core/iterutil@^0.3.0",
7374
"@core/unknownutil": "./mod.ts",
7475
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",

ensure.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,32 @@ import { assert } from "./assert.ts";
66
*
77
* It throws {@linkcode [assert].AssertError|AssertError} if the value does not satisfy the predicate.
88
*
9+
* @param x The value to be ensured.
10+
* @param pred The predicate function to test the value against.
11+
* @returns The input value `x`.
12+
*
913
* ```ts
1014
* import { ensure, is } from "@core/unknownutil";
1115
*
1216
* const a: unknown = "hello";
1317
* const _: string = ensure(a, is.String);
1418
* ```
1519
*
16-
* @param x The value to be ensured.
17-
* @param pred The predicate function to test the value against.
18-
* @param options Optional configuration for the assertion.
19-
* @returns The input value `x`.
20+
* Use {@linkcode https://jsr.io/@core/errorutil/doc/alter/~/alter|@core/errorutil/alter.alter} to alter error.
21+
*
22+
* ```ts
23+
* import { alter } from "@core/errorutil/alter";
24+
* import { ensure, is } from "@core/unknownutil";
25+
*
26+
* const a: unknown = 42;
27+
* const _: string = alter(() => ensure(a, is.String), new Error("a is not a string"));
28+
* // Error: a is not a string
29+
* ```
2030
*/
2131
export function ensure<T>(
2232
x: unknown,
2333
pred: Predicate<T>,
24-
options: { message?: string; name?: string } = {},
2534
): T {
26-
assert(x, pred, options);
35+
assert(x, pred);
2736
return x;
2837
}

ensure_test.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { assertStrictEquals, assertThrows } from "@std/assert";
2-
import {
3-
AssertError,
4-
defaultAssertMessageFactory,
5-
setAssertMessageFactory,
6-
} from "./assert.ts";
2+
import { AssertError } from "./assert.ts";
73
import { ensure } from "./ensure.ts";
84

95
const x: unknown = Symbol("x");
@@ -28,40 +24,4 @@ Deno.test("ensure", async (t) => {
2824
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
2925
);
3026
});
31-
32-
await t.step(
33-
"throws an `AssertError` on false predicate with a custom name",
34-
() => {
35-
assertThrows(
36-
() => ensure(x, falsePredicate, { name: "hello world" }),
37-
AssertError,
38-
`Expected hello world that satisfies the predicate falsePredicate, got symbol: undefined`,
39-
);
40-
},
41-
);
42-
43-
await t.step(
44-
"throws an `AssertError` with a custom message on false predicate",
45-
() => {
46-
assertThrows(
47-
() => ensure(x, falsePredicate, { message: "Hello" }),
48-
AssertError,
49-
"Hello",
50-
);
51-
},
52-
);
53-
});
54-
55-
Deno.test("setAssertMessageFactory", async (t) => {
56-
setAssertMessageFactory((x, pred) => `Hello ${typeof x} ${pred.name}`);
57-
58-
await t.step("change `AssertError` message on `ensure` failure", () => {
59-
assertThrows(
60-
() => ensure(x, falsePredicate),
61-
AssertError,
62-
"Hello symbol falsePredicate",
63-
);
64-
});
65-
66-
setAssertMessageFactory(defaultAssertMessageFactory);
6727
});

0 commit comments

Comments
 (0)