forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeModelStoreHelper.ts
More file actions
62 lines (56 loc) · 1.51 KB
/
TreeModelStoreHelper.ts
File metadata and controls
62 lines (56 loc) · 1.51 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
import { ITreeItem } from '../types/ITreeItem';
export default abstract class TreeModelStoreHelper {
static getItemRecursive<T extends ITreeItem<any>>(
tasks: T[],
condition: (task: T) => boolean
): T | undefined {
for (const task of tasks) {
if (condition(task)) {
return task;
}
if (Array.isArray(task.children)) {
const found = this.getItemRecursive(task.children, condition);
if (found) {
return found;
}
}
}
return undefined;
}
static getFlatItemsRecursive<T extends ITreeItem<any>>(
tree: T[],
condition: (task: T) => boolean
): T[] {
const result: T[] = [];
this.getFlatItemsRecursiveBase(tree, condition, result);
return result;
}
static getFlatItemsRecursiveBase<T extends ITreeItem<any>>(
tasks: T[],
condition: (task: T) => boolean,
result: T[]
): T[] {
for (const task of tasks) {
if (condition(task)) {
result.push(task);
}
if (Array.isArray(task.children)) {
this.getFlatItemsRecursiveBase(task.children, condition, result);
}
}
return result;
}
static deleteItems<T extends ITreeItem<any>>(
tasks: T[],
condition: (task: T) => boolean
): T[] {
const result = tasks.filter((t) => !condition(t));
for (let i = 0; i < result.length; i++) {
const task = tasks[i];
if (Array.isArray(task.children)) {
tasks[i].children = this.deleteItems(task.children, condition);
}
}
return result;
}
}