1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17interface TreeNode { 18 name: string; 19 parent?: TreeNode; 20 children?: TreeNode[]; 21} 22 23type FilterType = (node: TreeNode | undefined | null) => boolean; 24 25class TreeUtils { 26 static findDescendantNode(node: TreeNode, isTargetNode: FilterType): TreeNode | undefined { 27 if (isTargetNode(node)) { 28 return node; 29 } 30 31 if (!node.children) { 32 return; 33 } 34 35 for (const child of node.children) { 36 const target = TreeUtils.findDescendantNode(child, isTargetNode); 37 if (target) { 38 return target; 39 } 40 } 41 42 return undefined; 43 } 44 45 static findAncestorNode(node: TreeNode, isTargetNode: FilterType): TreeNode | undefined { 46 let ancestor = node.parent; 47 48 while (ancestor && !isTargetNode(ancestor)) { 49 ancestor = ancestor.parent; 50 } 51 52 return ancestor; 53 } 54 55 static makeNodeFilter(filterString: string): FilterType { 56 const filter = (item: TreeNode | undefined | null) => { 57 if (item) { 58 const regex = new RegExp(filterString, 'i'); 59 return filterString.length === 0 || regex.test(`${item.name}`); 60 } 61 return false; 62 }; 63 return filter; 64 } 65} 66 67export {TreeNode, TreeUtils, FilterType}; 68