-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathensure.ts
More file actions
28 lines (27 loc) · 773 Bytes
/
ensure.ts
File metadata and controls
28 lines (27 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import type { Predicate } from "./type.ts";
import { assert } from "./assert.ts";
/**
* Ensures that the given value satisfies the provided predicate.
*
* It throws {@linkcode [assert].AssertError|AssertError} if the value does not satisfy the predicate.
*
* ```ts
* import { ensure, is } from "@core/unknownutil";
*
* const a: unknown = "hello";
* const _: string = ensure(a, is.String);
* ```
*
* @param x The value to be ensured.
* @param pred The predicate function to test the value against.
* @param options Optional configuration for the assertion.
* @returns The input value `x`.
*/
export function ensure<T>(
x: unknown,
pred: Predicate<T>,
options: { message?: string; name?: string } = {},
): T {
assert(x, pred, options);
return x;
}