Skip to content

Commit 7025a4f

Browse files
committed
💪 Individually define utility functions
1 parent f0b96b6 commit 7025a4f

File tree

7 files changed

+145
-105
lines changed

7 files changed

+145
-105
lines changed

util.ts assert.ts

-46
Original file line numberDiff line numberDiff line change
@@ -92,49 +92,3 @@ export function assert<T>(
9292
);
9393
}
9494
}
95-
96-
/**
97-
* Ensures that the given value satisfies the provided predicate.
98-
*
99-
* ```ts
100-
* import { ensure, is } from "@core/unknownutil";
101-
*
102-
* const a: unknown = "hello";
103-
* const _: string = ensure(a, is.String);
104-
* ```
105-
*
106-
* @param x The value to be ensured.
107-
* @param pred The predicate function to test the value against.
108-
* @param options Optional configuration for the assertion.
109-
* @returns The input value `x`.
110-
* @throws {AssertError} If the value does not satisfy the predicate.
111-
*/
112-
export function ensure<T>(
113-
x: unknown,
114-
pred: Predicate<T>,
115-
options: { message?: string; name?: string } = {},
116-
): T {
117-
assert(x, pred, options);
118-
return x;
119-
}
120-
121-
/**
122-
* Returns the input value if it satisfies the provided predicate, or `undefined` otherwise.
123-
*
124-
* ```ts
125-
* import { is, maybe } from "@core/unknownutil";
126-
*
127-
* const a: unknown = "hello";
128-
* const _: string = maybe(a, is.String) ?? "default value";
129-
* ```
130-
*
131-
* @param x The value to be tested.
132-
* @param pred The predicate function to test the value against.
133-
* @returns The input value `x` if it satisfies the predicate, or `undefined` otherwise.
134-
*/
135-
export function maybe<T>(
136-
x: unknown,
137-
pred: Predicate<T>,
138-
): T | undefined {
139-
return pred(x) ? x : undefined;
140-
}

assert_test.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { assertThrows } from "@std/assert";
2+
import {
3+
assert,
4+
AssertError,
5+
defaultAssertMessageFactory,
6+
setAssertMessageFactory,
7+
} from "./assert.ts";
8+
9+
const x: unknown = Symbol("x");
10+
11+
function truePredicate(_x: unknown): _x is string {
12+
return true;
13+
}
14+
15+
function falsePredicate(_x: unknown): _x is string {
16+
return false;
17+
}
18+
19+
Deno.test("assert", async (t) => {
20+
await t.step("does nothing on true predicate", () => {
21+
assert(x, truePredicate);
22+
});
23+
24+
await t.step("throws an `AssertError` on false predicate", () => {
25+
assertThrows(
26+
() => assert(x, falsePredicate),
27+
AssertError,
28+
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
29+
);
30+
});
31+
32+
await t.step(
33+
"throws an `AssertError` on false predicate with a custom name",
34+
() => {
35+
assertThrows(
36+
() => assert(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+
() => assert(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 `assert` failure", () => {
59+
assertThrows(
60+
() => assert(x, falsePredicate),
61+
AssertError,
62+
"Hello symbol falsePredicate",
63+
);
64+
});
65+
66+
setAssertMessageFactory(defaultAssertMessageFactory);
67+
});

ensure.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Predicate } from "./type.ts";
2+
import { assert } from "./assert.ts";
3+
4+
/**
5+
* Ensures that the given value satisfies the provided predicate.
6+
*
7+
* ```ts
8+
* import { ensure, is } from "@core/unknownutil";
9+
*
10+
* const a: unknown = "hello";
11+
* const _: string = ensure(a, is.String);
12+
* ```
13+
*
14+
* @param x The value to be ensured.
15+
* @param pred The predicate function to test the value against.
16+
* @param options Optional configuration for the assertion.
17+
* @returns The input value `x`.
18+
* @throws {AssertError} If the value does not satisfy the predicate.
19+
*/
20+
export function ensure<T>(
21+
x: unknown,
22+
pred: Predicate<T>,
23+
options: { message?: string; name?: string } = {},
24+
): T {
25+
assert(x, pred, options);
26+
return x;
27+
}

util_test.ts ensure_test.ts

+2-58
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { assertStrictEquals, assertThrows } from "@std/assert";
22
import {
3-
assert,
43
AssertError,
54
defaultAssertMessageFactory,
6-
ensure,
7-
maybe,
85
setAssertMessageFactory,
9-
} from "./util.ts";
6+
} from "./assert.ts";
7+
import { ensure } from "./ensure.ts";
108

119
const x: unknown = Symbol("x");
1210

@@ -18,42 +16,6 @@ function falsePredicate(_x: unknown): _x is string {
1816
return false;
1917
}
2018

21-
Deno.test("assert", async (t) => {
22-
await t.step("does nothing on true predicate", () => {
23-
assert(x, truePredicate);
24-
});
25-
26-
await t.step("throws an `AssertError` on false predicate", () => {
27-
assertThrows(
28-
() => assert(x, falsePredicate),
29-
AssertError,
30-
`Expected a value that satisfies the predicate falsePredicate, got symbol: undefined`,
31-
);
32-
});
33-
34-
await t.step(
35-
"throws an `AssertError` on false predicate with a custom name",
36-
() => {
37-
assertThrows(
38-
() => assert(x, falsePredicate, { name: "hello world" }),
39-
AssertError,
40-
`Expected hello world that satisfies the predicate falsePredicate, got symbol: undefined`,
41-
);
42-
},
43-
);
44-
45-
await t.step(
46-
"throws an `AssertError` with a custom message on false predicate",
47-
() => {
48-
assertThrows(
49-
() => assert(x, falsePredicate, { message: "Hello" }),
50-
AssertError,
51-
"Hello",
52-
);
53-
},
54-
);
55-
});
56-
5719
Deno.test("ensure", async (t) => {
5820
await t.step("returns `x` as-is on true predicate", () => {
5921
assertStrictEquals(ensure(x, truePredicate), x);
@@ -90,27 +52,9 @@ Deno.test("ensure", async (t) => {
9052
);
9153
});
9254

93-
Deno.test("maybe", async (t) => {
94-
await t.step("returns `x` as-is on true predicate", () => {
95-
assertStrictEquals(maybe(x, truePredicate), x);
96-
});
97-
98-
await t.step("returns `undefined` on false predicate", () => {
99-
assertStrictEquals(maybe(x, falsePredicate), undefined);
100-
});
101-
});
102-
10355
Deno.test("setAssertMessageFactory", async (t) => {
10456
setAssertMessageFactory((x, pred) => `Hello ${typeof x} ${pred.name}`);
10557

106-
await t.step("change `AssertError` message on `assert` failure", () => {
107-
assertThrows(
108-
() => assert(x, falsePredicate),
109-
AssertError,
110-
"Hello symbol falsePredicate",
111-
);
112-
});
113-
11458
await t.step("change `AssertError` message on `ensure` failure", () => {
11559
assertThrows(
11660
() => ensure(x, falsePredicate),

maybe.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Predicate } from "./type.ts";
2+
3+
/**
4+
* Returns the input value if it satisfies the provided predicate, or `undefined` otherwise.
5+
*
6+
* ```ts
7+
* import { is, maybe } from "@core/unknownutil";
8+
*
9+
* const a: unknown = "hello";
10+
* const _: string = maybe(a, is.String) ?? "default value";
11+
* ```
12+
*
13+
* @param x The value to be tested.
14+
* @param pred The predicate function to test the value against.
15+
* @returns The input value `x` if it satisfies the predicate, or `undefined` otherwise.
16+
*/
17+
export function maybe<T>(
18+
x: unknown,
19+
pred: Predicate<T>,
20+
): T | undefined {
21+
return pred(x) ? x : undefined;
22+
}

maybe_test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { assertStrictEquals } from "@std/assert";
2+
import { maybe } from "./maybe.ts";
3+
4+
const x: unknown = Symbol("x");
5+
6+
function truePredicate(_x: unknown): _x is string {
7+
return true;
8+
}
9+
10+
function falsePredicate(_x: unknown): _x is string {
11+
return false;
12+
}
13+
14+
Deno.test("maybe", async (t) => {
15+
await t.step("returns `x` as-is on true predicate", () => {
16+
assertStrictEquals(maybe(x, truePredicate), x);
17+
});
18+
19+
await t.step("returns `undefined` on false predicate", () => {
20+
assertStrictEquals(maybe(x, falsePredicate), undefined);
21+
});
22+
});

mod.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@
241241
*/
242242

243243
export type * from "./type.ts";
244+
244245
export * from "./as/mod.ts";
245246
export * from "./is/mod.ts";
246-
export * from "./util.ts";
247+
248+
export * from "./assert.ts";
249+
export * from "./ensure.ts";
250+
export * from "./maybe.ts";

0 commit comments

Comments
 (0)