-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirectory.ts
More file actions
54 lines (48 loc) · 1.32 KB
/
directory.ts
File metadata and controls
54 lines (48 loc) · 1.32 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
44
45
46
47
48
49
50
51
52
53
54
import Node from './node'
import IIterator from './iiterator'
class Directory extends Node {
private children: Array<Node>;
constructor(n: string, p?: Directory){
super(n,p);
this.children = [];
}
public getAbsoluteName() : string {
return super.getAbsoluteName() + "/";
}
public add(n: Node) : void {
this.children.push(n);
}
public find(s: string) : Array<string> {
let result = new Array<string>();
if (this.name.indexOf(s) != -1){
result.push(this.getAbsoluteName());
}
for (let child of this.children){
result.push(...child.find(s)); // TODO: Mitch to rewrite this more functionally?
}
return result;
}
public iterator() : IIterator<Node> {
return new DirectoryIterator(this);
}
public getChildren(){ // can we avoid making this method public?
return this.children;
}
}
class DirectoryIterator implements IIterator<Node> {
private files : Array<Node> ;
private fileCount : number;
constructor(d: Directory) {
this.files = d.getChildren();
this.fileCount = 0;
}
public first() : void { this.fileCount = 0; }
public next() : void { this.fileCount++; }
public isDone() : boolean {
return this.fileCount == this.files.length;
}
public current() : Node {
return this.files[this.fileCount];
}
}
export default Directory