Repository navigation
Expand file tree
/
Copy patharray-not-includes.assert.ts
More file actions
62 lines (56 loc) · 1.73 KB
/
Copy patharray-not-includes.assert.ts
File metadata and controls
62 lines (56 loc) · 1.73 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
54
55
56
57
58
59
60
61
62
import { arrayNotIncluding } from "./array-not-includes.match.js";
import { AssertionError } from "../../assertion-error.js";
import { desc } from "../../describe/describe.js";
export function assertArrayNotIncludes<
TArray extends unknown[],
const E extends TArray[number],
>(value: TArray, element: E, message?: string): asserts value is TArray;
export function assertArrayNotIncludes<const E>(
value: unknown,
element: E,
message?: string,
): asserts value is unknown[];
/**
* Assert that an array does not include a specific element, with type
* narrowing.
* Note that this is an identity match, so an object in an array only fails the
* assertion by being a reference to the same object, rather than equivalent in
* value to another object reference.
* Note that absence has no type-level witness, so this narrows an unknown value
* to an array and stops there.
* @example
* ```ts
* import { assertArrayNotIncludes } from "@kensio/smartass"
*
* const value: unknown = ["editor", "viewer"];
*
* assertArrayNotIncludes(value, "admin");
*
* // value is now narrowed to an array
* // unknown[]
*
* ```
*/
export function assertArrayNotIncludes(
value: unknown,
element: unknown,
message?: string,
): void {
const matcher = arrayNotIncluding(element);
if (!matcher.isMatch(value)) {
throw new AssertionError(
message ?? buildArrayNotIncludesMessage(value, element),
value,
matcher.represent(),
);
}
}
function buildArrayNotIncludesMessage(
value: unknown,
element: unknown,
): string {
if (!Array.isArray(value)) {
return `Expected ${desc(value)} to be an array not including ${desc(element)}.`;
}
return `Expected ${desc(value)} not to include ${desc(element)}, but it did.`;
}