-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiterable_groups.js
More file actions
52 lines (44 loc) · 835 Bytes
/
iterable_groups.js
File metadata and controls
52 lines (44 loc) · 835 Bytes
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
class Group {
constructor() {
this.group = [];
}
add(value) {
if (!this.has(value)) {
this.group.push(value);
}
}
delete(value) {
this.group = this.group.filter((v) => v != value);
}
has(value) {
return this.group.includes(value);
}
static from(collection) {
let group = new Group();
for (let value of collection) {
group.add(value);
}
return group;
}
[Symbol.iterator]() {
return new GroupIterator(this);
}
}
class GroupIterator {
constructor(group) {
this.group = group;
this.position = 0;
}
next() {
if (this.position >= this.group.group.length) {
return { done: true };
} else {
let result = { value: this.group.group[this.position], done: false };
this.position++;
return result;
}
}
}
for (let value of Group.from(['a', 'b', 'c'])) {
console.log(value);
}