generated from mekefly/vitest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (63 loc) · 1.5 KB
/
index.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
export class MySet<T> implements Set<T> {
map: Map<T, T> = new Map();
constructor(iterable: Iterable<T>);
constructor();
constructor(iterable: null);
constructor(iterable?: null | Iterable<T>) {
if (!iterable) {
return;
}
for (const value of iterable) {
this.add(value);
}
}
public get size(): number {
return this.map.size;
}
add(value: T): this {
this.map.set(value, value);
return this;
}
clear(): void {
this.map.clear();
}
delete(value: T): boolean {
return this.map.delete(value);
}
forEach(
callbackfn: (value: T, value2: T, set: Set<T>) => void,
thisArg?: any
): void {
this.map.forEach((value, key, map) => {
callbackfn(key, key, this);
}, thisArg);
}
has(value: T): boolean {
return !!this.map.get(value);
}
entries(): IterableIterator<[T, T]> {
return new SetEntries(this.map);
}
keys(): IterableIterator<T> {
return this.map.keys();
}
values(): IterableIterator<T> {
return this.map.keys();
}
[Symbol.iterator](): IterableIterator<T> {
return this.keys();
}
[Symbol.toStringTag]: string = "Set";
}
class SetEntries<T> implements IterableIterator<[T, T]> {
private mapEntries: IterableIterator<[T, T]>;
constructor(map: Map<T, T>) {
this.mapEntries = map.entries();
}
[Symbol.iterator](): IterableIterator<[T, T]> {
return this.mapEntries;
}
next(...args: [] | [undefined]): IteratorResult<[T, T], any> {
return this.mapEntries.next(...args);
}
}