-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-includes.assert.ts
More file actions
39 lines (35 loc) · 1.1 KB
/
Copy pathstring-includes.assert.ts
File metadata and controls
39 lines (35 loc) · 1.1 KB
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
29
30
31
32
33
34
35
36
37
38
39
import { AssertionError } from "../../assertion-error.js";
import { desc, repr } from "../../describe/describe.js";
import { stringIncluding } from "./string-includes.match.js";
import type { StringIncludingAssertion } from "./string-includes.type.js";
export function assertStringIncludes<
TActual extends string,
const TSubstring extends string,
>(
value: TActual,
substring: TSubstring,
message?: string,
): asserts value is StringIncludingAssertion<TActual, TSubstring>;
export function assertStringIncludes<const TSubstring extends string>(
value: unknown,
substring: TSubstring,
message?: string,
): asserts value is `${string}${TSubstring}${string}`;
/**
* Assert that a string includes a given substring, with type narrowing.
*/
export function assertStringIncludes(
value: unknown,
substring: string,
message?: string,
): void {
const matcher = stringIncluding(substring);
if (!matcher.matches(value)) {
throw new AssertionError(
message ??
`Expected ${desc(value)} to include ${repr(substring)}, but it did not.`,
value,
matcher.represent(),
);
}
}