add findInTree function in utils

This commit is contained in:
Géry Debongnie
2019-02-01 12:47:01 +01:00
parent c46bad6e35
commit 37a8b3c7e7
2 changed files with 35 additions and 1 deletions
+20
View File
@@ -83,3 +83,23 @@ export function debounce(
}
};
}
interface Tree<T> {
children: T[];
}
export function findInTree<T extends Tree<T>>(
tree: T,
predicate: (t: T) => boolean
): T | null {
if (predicate(tree)) {
return tree;
}
for (let child of tree.children) {
let match = findInTree(child, predicate);
if (match) {
return match;
}
}
return null;
}