From 5fbbe208eec38871ba79dd15970ac8c1b688e609 Mon Sep 17 00:00:00 2001 From: uri Date: Fri, 3 Jan 2025 18:37:56 +0200 Subject: [PATCH] exhaustive-tree-search --- src/tree.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tree.ts b/src/tree.ts index 83aa1b3..20fa5d2 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -20,3 +20,12 @@ export const findInTree = } return null; }; + +export const findInTreeExhaustive = + (predicate: (t: T) => boolean, children: (t: T) => T[]) => (t: T): T[] => { + const results = (predicate(t)) ? [t] : []; + for (const child of children(t)) { + results.push(...findInTreeExhaustive(predicate, children)(child)); + } + return results; + };