-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathNode.ts
More file actions
91 lines (70 loc) · 2.03 KB
/
PathNode.ts
File metadata and controls
91 lines (70 loc) · 2.03 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
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
import { IPathNode } from "./IPathNode";
export class PathNode implements IPathNode {
private readonly _key: string;
private _children: Map<string, IPathNode> = new Map<string, IPathNode>();
private _parent: IPathNode | null = null;
private _wildcard: IPathNode | null = null;
private _isWildcard = false;
private _isLeaf = false;
private _data?: unknown = undefined;
constructor(key: string) {
this._key = key;
}
get key(): string {
return this._key;
}
get parent(): IPathNode | null {
return this._parent;
}
get wildcard(): IPathNode | null {
return this._wildcard;
}
get isWildcard(): boolean {
return this._isWildcard;
}
set isWildcard(v: boolean) {
this._isWildcard = v;
}
get isLeaf(): boolean {
return this._isLeaf;
}
set isLeaf(v: boolean) {
this._isLeaf = v;
}
get data(): unknown | undefined {
return this._data;
}
set data(data: unknown | undefined) {
if (!this.isLeaf) {
throw new Error("Cannot store data in non-leaf node");
}
this._data = data;
}
getChild(key: string) : IPathNode | null {
return this._children.get(key) || null;
}
addChild(child: IPathNode, wildcard: boolean): void {
if (wildcard) {
this._wildcard = child;
child.isWildcard = true;
} else {
this._children.set(child.key, child);
}
child.attachToParent(this);
}
attachToParent(parent: IPathNode): void {
this._parent = parent;
}
get path(): string {
const output: string[] = [];
// eslint-disable-next-line @typescript-eslint/no-this-alias
for (let node : IPathNode | null = this; node !== null; node = node.parent) {
if (node.isWildcard) {
output.unshift(':' + node.key);
} else {
output.unshift(node.key);
}
}
return output.join('/');
}
}