-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-not-includes.assert.ts
More file actions
53 lines (46 loc) · 1.48 KB
/
Copy pathstring-not-includes.assert.ts
File metadata and controls
53 lines (46 loc) · 1.48 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { stringNotIncluding } from "./string-not-includes.match.js";
import { AssertionError } from "../../assertion-error.js";
import { desc, repr } from "../../describe/describe.js";
type StringNotIncluding<
TActual extends string,
TSubstring extends string,
> = TActual extends `${string}${TSubstring}${string}` ? never : TActual;
export function assertStringNotIncludes<
TActual extends string,
const TSubstring extends string,
>(
value: TActual,
substring: TSubstring,
message?: string,
): asserts value is StringNotIncluding<TActual, TSubstring>;
export function assertStringNotIncludes<const TSubstring extends string>(
value: unknown,
substring: TSubstring,
message?: string,
): asserts value is Exclude<string, `${string}${TSubstring}${string}`>;
/**
* Assert that a string does not include a given substring, with type narrowing.
*/
export function assertStringNotIncludes(
value: unknown,
substring: string,
message?: string,
): void {
const matcher = stringNotIncluding(substring);
if (!matcher.matches(value)) {
throw new AssertionError(
message ?? buildStringNotIncludesMessage(value, substring),
value,
matcher.represent(),
);
}
}
function buildStringNotIncludesMessage(
value: unknown,
substring: string,
): string {
if (typeof value !== "string") {
return `Expected ${desc(value)} to be a string not including ${repr(substring)}.`;
}
return `Expected ${desc(value)} not to include ${repr(substring)}, but it did.`;
}