-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-not-includes.match.ts
More file actions
45 lines (44 loc) · 1.36 KB
/
Copy patharray-not-includes.match.ts
File metadata and controls
45 lines (44 loc) · 1.36 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
import { createMatcher } from "../../match/match.js";
import { desc, repr } from "../../describe/describe.js";
import {
arrayNotIncludingMatcher,
type ArrayNotIncludingMatcher,
} from "./array-not-includes.type.js";
/**
* Matcher for an array that does not include a specific single element.
* Matchers are applied through assertObjectMatches, where they narrow the
* corresponding property type.
* Type information that already exists in the calling scope is incorporated.
* @example
* ```ts
* import { arrayNotIncluding, assertObjectMatches } from "@kensio/smartass";
*
* const value: unknown = {
* roles: ["editor", "viewer"],
* };
*
* assertObjectMatches(value, {
* roles: arrayNotIncluding("admin"),
* });
*
* // value is now narrowed to an object with an array of roles
* // {
* // roles: unknown[];
* // }
* ```
*/
export function arrayNotIncluding<const E>(
element: E,
): ArrayNotIncludingMatcher<E> {
return {
...createMatcher(
(value): value is unknown[] =>
Array.isArray(value) && !value.includes(element),
() => `array not including ${desc(element)}`,
() => `[…,✗${repr(element)}✗,…]`,
),
// Runtime marker used only to make the matcher type nominal for type-level
// refinement dispatch. It is not part of the user-facing matcher behaviour.
[arrayNotIncludingMatcher]: element,
};
}