forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.ts
84 lines (63 loc) · 2.09 KB
/
collection.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { ActorPF2e } from "@actor";
import type { ItemPF2e } from "@item";
import { MockActor } from "./actor.ts";
import { MockItem } from "./item.ts";
/** In Foundry this is actually a subclass of Map, but it incompatibly extends it at several points. */
export class MockCollection<V> {
#map: Map<string, V>;
constructor(entries: [string, V][] = []) {
this.#map = new Map(entries);
}
[Symbol.iterator](): IterableIterator<V> {
return this.#map.values();
}
get size(): number {
return this.#map.size;
}
get contents(): V[] {
return Array.from(this.#map.values());
}
get(key: string): V | null {
return this.#map.get(key) ?? null;
}
set(key: string, value: V): MockCollection<V> {
this.#map.set(key, value);
return this;
}
has(key: string): boolean {
return this.#map.has(key);
}
find(predicate: (value: V) => boolean): V | undefined {
return this.contents.find(predicate);
}
some(predicate: (value: V) => boolean): boolean {
return this.contents.some(predicate);
}
filter(predicate: (value: V) => boolean): V[] {
return this.contents.filter(predicate);
}
map<T>(callback: (value: V) => T): T[] {
return this.contents.map(callback);
}
delete(key: string): boolean {
return this.#map.delete(key);
}
clear(): void {
this.#map.clear();
}
}
export class MockWorldCollection<V extends { readonly parent: null }> extends MockCollection<V> {}
export class MockActors extends MockWorldCollection<ActorPF2e<null>> {
tokens: Record<string, ActorPF2e | undefined> = {};
documentClass = MockActor as unknown as typeof ActorPF2e;
constructor(entries: [string, ActorPF2e<null>][] = []) {
super(entries);
}
}
export class MockItems extends MockWorldCollection<ItemPF2e<null>> {
tokens: Record<string, ActorPF2e | undefined> = {};
documentClass = MockItem as unknown as typeof ItemPF2e;
constructor(entries: [string, ItemPF2e<null>][] = []) {
super(entries);
}
}