-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-includes.assert.ts
More file actions
42 lines (38 loc) · 1.2 KB
/
Copy patharray-includes.assert.ts
File metadata and controls
42 lines (38 loc) · 1.2 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
import { arrayIncluding } from "./array-includes.match.js";
import { AssertionError } from "../../assertion-error.js";
import { desc } from "../../describe/describe.js";
import type { ArrayIncluding } from "./array-includes.type.js";
export function assertArrayIncludes<
TArray extends unknown[],
const E extends TArray[number],
>(
value: TArray,
element: E,
message?: string,
): asserts value is TArray & ArrayIncluding<TArray[number]>;
export function assertArrayIncludes<const E>(
value: unknown,
element: E,
message?: string,
): asserts value is ArrayIncluding<E>;
/**
* Assert that an array includes a specific element, with type narrowing.
* Note that this is an identity match, so objects in an array only fulfill the
* assertion by being a reference to the same object, rather than equivalent in
* value to another object reference.
*/
export function assertArrayIncludes(
value: unknown,
element: unknown,
message?: string,
): void {
const matcher = arrayIncluding(element);
if (!matcher.matches(value)) {
throw new AssertionError(
message ??
`Expected ${desc(value)} to include ${desc(element)}, but it did not.`,
value,
matcher.represent(),
);
}
}