generated from Himenon/template-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOperator.ts
115 lines (104 loc) · 4.43 KB
/
Operator.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
import type { Component, Children, HierarchicalData } from "./types";
import { Tree } from "./Tree";
import { split, splitKey } from "./Utils";
export class Operator<TreeKind extends string> {
private tree: Tree<TreeKind>;
constructor(private treeKind: TreeKind, private delimiter: string = "/") {
this.tree = new Tree(treeKind, ".");
}
private setToRootOrParent(component: Component, currentPathName: string, previousPathArray: string[], nextPathArray: string[]): void {
const childComponent = this.tree.getChildByPaths(nextPathArray, component.kind);
if (childComponent || nextPathArray.length === 0) {
return;
}
if (previousPathArray.length === 0 && nextPathArray.length === 1) {
this.tree.set(nextPathArray[0], component);
} else {
const parentComponent = this.tree.getChildByPaths(previousPathArray, this.treeKind);
parentComponent && parentComponent.set(currentPathName, component);
}
}
private removeFromRootOrParent(currentPathName: string, currentPathArray: string[], component: Component): void {
const previousPathArray: string[] = currentPathArray.slice(0, currentPathArray.length - 1);
if (previousPathArray.length === 0 && currentPathArray.length === 1) {
this.tree.remove(currentPathName, component);
} else {
const parentComponent = this.tree.getChildByPaths(previousPathArray, this.treeKind);
parentComponent && parentComponent.remove(currentPathName, component);
}
}
public getHierarchy(): HierarchicalData {
return this.tree.getHierarchy();
}
public getChildByPaths(path: string, kind: string): Component | undefined {
const pathArray = split(path, this.delimiter);
return this.tree.getChildByPaths(pathArray, kind);
}
public set(path: string, component: Component): void {
const pathArray = split(path, this.delimiter);
const pathArrayLength = pathArray.length;
pathArray.reduce<string[]>((previousPathArray, currentPathName, currentIndex) => {
const nextPathArray = previousPathArray.concat(currentPathName);
const isLastIndex = currentIndex === pathArrayLength - 1;
if (isLastIndex) {
this.setToRootOrParent(component, currentPathName, previousPathArray, nextPathArray);
} else {
const tree = new Tree(this.treeKind, currentPathName);
this.setToRootOrParent(tree, currentPathName, previousPathArray, nextPathArray);
}
return nextPathArray;
}, []);
}
public remove(path: string, kind: string): void {
const pathArray = split(path, this.delimiter);
const pathArrayLength = pathArray.length;
for (let i = 0; i <= pathArrayLength; i++) {
const currentPathArray = pathArray.slice(0, pathArrayLength - i);
const currentPathName = pathArray[pathArrayLength - i - 1];
if (!currentPathName) {
continue;
}
if (i === 0) {
const component = this.tree.getChildByPaths(currentPathArray, kind);
component && this.removeFromRootOrParent(currentPathName, currentPathArray, component);
} else {
const component = this.tree.getChildByPaths(currentPathArray, this.treeKind);
component && component.hasChildren() && this.removeFromRootOrParent(currentPathName, currentPathArray, component);
}
}
}
public copy(from: string, to: string, kind: string): boolean {
const fromComponent = this.getChildByPaths(from, kind);
const toComponent = this.getChildByPaths(to, kind);
if (toComponent || !fromComponent) {
return false;
}
this.set(to, fromComponent);
return true;
}
public move(from: string, to: string, kind: string): boolean {
const success = this.copy(from, to, kind);
if (success) {
this.remove(from, kind);
return true;
}
return false;
}
private getChildPaths(kind: string, children: Children, parentPath: string): string[] {
let pathArray: string[] = [];
Object.entries(children).forEach(([key, child]) => {
const subChildren = child.getChildren();
const [, pathName] = splitKey(key);
const nextPath = [parentPath, pathName].join(this.delimiter).replace(/\/$/, "");
if (subChildren) {
pathArray = pathArray.concat(this.getChildPaths(kind, subChildren, nextPath));
} else if (child.kind === kind) {
pathArray.push(nextPath);
}
});
return pathArray;
}
public getNodePaths(kind: string): string[] {
return this.getChildPaths(kind, this.tree.getChildren(), this.tree.name);
}
}