forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakSet.ts
More file actions
43 lines (37 loc) · 1.25 KB
/
WeakSet.ts
File metadata and controls
43 lines (37 loc) · 1.25 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
WeakSet = class WeakSet<T> {
public static [Symbol.species] = WeakSet;
public [Symbol.toStringTag] = "WeakSet";
private items = new LuaTable<T, boolean>();
constructor(values?: Iterable<T> | T[]) {
setmetatable(this.items, { __mode: "k" });
if (values === undefined) return;
const iterable = values as Iterable<T>;
if (iterable[Symbol.iterator]) {
// Iterate manually because WeakSet is compiled with ES5 which doesn't support Iterables in for...of
const iterator = iterable[Symbol.iterator]();
while (true) {
const result = iterator.next();
if (result.done) {
break;
}
this.items.set(result.value, true);
}
} else {
for (const value of values as T[]) {
this.items.set(value, true);
}
}
}
public add(value: T): this {
this.items.set(value, true);
return this;
}
public delete(value: T): boolean {
const contains = this.has(value);
this.items.set(value, undefined);
return contains;
}
public has(value: T): boolean {
return this.items.get(value) === true;
}
};