This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
/
ObjectSet.ts
119 lines (106 loc) · 3.17 KB
/
ObjectSet.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
export abstract class ObjectSet<T> implements Set<T> {
/**
* Returns the string literal 'Set' for use by Object.prototype.toString.
* This allows for identifying Sets without checking constructor identity.
*/
public readonly [Symbol.toStringTag]: 'Set' = 'Set';
protected _values: Array<T> = [];
/**
* Creates a new ObjectSet and optionally seeds it with values.
*
* @param iterable An optional iterable of values to add to the set.
*/
constructor(iterable?: Iterable<T>) {
if (iterable) {
for (let item of iterable) {
this.add(item);
}
}
}
/**
* Add a value to the set. If the value is already contained in the set, it
* will not be added a second time.
*
* @param value The value to add
*/
add(value: T): this {
if (!this.has(value)) {
this._values.push(value);
}
return this;
}
/**
* Remove all values from the set.
*/
clear(): void {
this._values = [];
}
/**
* Removes a particular value from the set. If the value was contained in
* the set prior to this method being called, `true` will be returned; if
* the value was not in the set, `false` will be returned. In either case,
* the value provided will not be in the set after this method returns.
*
* @param value The value to remove from the set.
*/
abstract delete(value: T): boolean;
/**
* Returns an iterable two-member tuples for each item in the set, where
* the item is provided twice.
*
* Part of the ES2015 Set specification for compatibility with Map objects.
*/
entries(): IterableIterator<[T, T]> {
return this._values.map<[T, T]>(
value => [value, value]
)[Symbol.iterator]();
}
/**
* Invokes a callback once for each member of the set.
*
* @param callback The function to invoke with each set member
* @param thisArg The `this` context on which to invoke the callback
*/
forEach(
callback: (
value: T,
value2: T,
set: Set<T>
) => void,
thisArg?: any
): void {
this._values.forEach((value, index, array) => {
callback.call(thisArg, value, value, this);
}, thisArg);
}
/**
* Determines if a provided value is already a member of the set.
*
* @param value The value against which set members should be checked
*/
abstract has(value: T): boolean;
/**
* Returns an IterableIterator of each member of the set.
*/
keys(): IterableIterator<T> {
return this[Symbol.iterator]();
}
/**
* Returns the number of members in the set.
*/
get size(): number {
return this._values.length;
}
/**
* Returns an IterableIterator of each member of the set.
*/
values(): IterableIterator<T> {
return this[Symbol.iterator]();
}
/**
* Returns an IterableIterator of each member of the set.
*/
[Symbol.iterator](): IterableIterator<T> {
return this._values[Symbol.iterator]();
}
}