@the-w1zard/dx 1.0.11 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+
2
+ export class WS2I<T extends WeakKey = A> extends WeakSet<T> implements WS2<T> {
3
+ private readonly _tracked = new Set<T>();
4
+
5
+ constructor(values?: readonly T[] | Iterable<T> | null) {
6
+ super(values == null ? undefined : Array.from(values));
7
+ }
8
+ override add(value: T): this {
9
+ return super.add(value);
10
+ }
11
+
12
+ override delete(value: T): boolean {
13
+ this._tracked.delete(value);
14
+ return super.delete(value);
15
+ }
16
+
17
+ override has(value: T): boolean {
18
+ return super.has(value);
19
+ }
20
+
21
+ addIfAbsent(value: T): boolean {
22
+ if (this.has(value)) return false;
23
+ this.add(value);
24
+ return true;
25
+ }
26
+
27
+ toggle(value: T): boolean {
28
+ if (this.has(value)) {
29
+ this.delete(value);
30
+ return false;
31
+ }
32
+ this.add(value);
33
+ return true;
34
+ }
35
+
36
+ deleteIfPresent(value: T): boolean {
37
+ return this.delete(value);
38
+ }
39
+
40
+ hasAll(values: Iterable<T>): boolean {
41
+ for (const value of values) {
42
+ if (!this.has(value)) return false;
43
+ }
44
+ return true;
45
+ }
46
+
47
+ hasAny(values: Iterable<T>): boolean {
48
+ for (const value of values) {
49
+ if (this.has(value)) return true;
50
+ }
51
+ return false;
52
+ }
53
+
54
+ addAll(values: Iterable<T>): this {
55
+ for (const value of values) this.add(value);
56
+ return this;
57
+ }
58
+
59
+ deleteAll(values: Iterable<T>): N {
60
+ let count = 0;
61
+ for (const value of values) {
62
+ if (this.delete(value)) count++;
63
+ }
64
+ return count as N;
65
+ }
66
+
67
+ replace(oldValue: T, newValue: T): boolean {
68
+ if (!this.has(oldValue)) return false;
69
+ this.delete(oldValue);
70
+ this.add(newValue);
71
+ return true;
72
+ }
73
+
74
+ track(value: T): this {
75
+ this.add(value);
76
+ this._tracked.add(value);
77
+ return this;
78
+ }
79
+
80
+ untrack(value: T): boolean {
81
+ return this._tracked.delete(value);
82
+ }
83
+
84
+ trackedKeys(): T[] {
85
+ return Array.from(this._tracked);
86
+ }
87
+
88
+ forEachTracked(callback: (value: T, set: this) => void, thisArg?: A): void {
89
+ this._tracked.forEach(value => {
90
+ if (this.has(value)) callback.call(thisArg, value, this);
91
+ else this._tracked.delete(value);
92
+ });
93
+ }
94
+
95
+ clearTracked(): void {
96
+ for (const value of this._tracked) super.delete(value);
97
+ this._tracked.clear();
98
+ }
99
+ }
100
+
101
+ (globalThis as A).WS2 = WS2I as WS2C;