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
37 lines (35 loc) · 904 Bytes
/
TreeModelStoreHelper.ts
File metadata and controls
37 lines (35 loc) · 904 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
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 getItemsRecursive<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.getItemsRecursive(task.children, condition, result);
}
}
return result;
}
}