-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-includes-all.match.ts
More file actions
60 lines (51 loc) · 1.71 KB
/
Copy patharray-includes-all.match.ts
File metadata and controls
60 lines (51 loc) · 1.71 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
import { createMatcher } from "../../match/match.js";
import { desc, repr } from "../../describe/describe.js";
import type {
ArrayIncludingAll,
ArrayIncludingAllElement,
ArrayIncludingAllMatcher,
} from "./array-includes-all.type.js";
/**
* Matcher for an array including all specified elements.
* Elements can appear in any order and do not need to be contiguous.
* Repeated required elements must appear at least that many times.
*/
export function arrayIncludingAll<const E extends readonly unknown[]>(
elements: E,
): ArrayIncludingAllMatcher<ArrayIncludingAllElement<E>, E["length"]> {
return createMatcher(
(
value,
): value is ArrayIncludingAll<ArrayIncludingAllElement<E>, E["length"]> =>
Array.isArray(value) && includesAll(value, elements),
() => `array including all of ${desc(elements)}`,
() => `[…,${reprArrayElements(elements)},…]`,
);
}
function includesAll(
value: readonly unknown[],
elements: readonly unknown[],
): boolean {
const remaining = [...value];
return elements.every((element) => {
const index = remaining.findIndex((candidate) =>
sameValueZero(candidate, element),
);
if (index === -1) {
return false;
}
remaining.splice(index, 1);
return true;
});
}
function sameValueZero(left: unknown, right: unknown): boolean {
return left === right || (Number.isNaN(left) && Number.isNaN(right));
}
function reprArrayElements(values: readonly unknown[]): string {
if (values.length <= 5) {
return values.map((value) => repr(value)).join(",");
}
const first = values.slice(0, 3).map((value) => repr(value));
const last = values.slice(-1).map((value) => repr(value));
return [...first, "…", ...last].join(",");
}