I have a file tree with this type
interface FileTreeProps {
name: string;
isMat?: boolean;
children?: FileTreeProps[];
}
After the user inputs and submits the query I want a filtered tree where the endpoints lead to the child having the type isMat
. The first two children are parent folders and will never contain isMat = true
. How do I go about filtering this?
I already have the code for checking similarity between two strings, and I only want to return that child if the similarity is greater than 0.25;
Example of a typical file structure (the paint brushes represent the isMat nodes):
and the object:
{
"name": "SubtlePBR",
"children": [
{
"name": "assets",
"children": [
{
"name": "minecraft",
"children": [
{
"name": "textures",
"children": [
{
"name": "block",
"children": [
{
"name": "acacia_leaves",
"children": [],
"isMat": true
},
{
"name": "acacia_log",
"children": [],
"isMat": true
}
]
}
]
}
]
}
]
}
]
}
After a few good hours I've figured it out:
function filterTree(tree: FileTreeProps): FileTreeProps | undefined {
const filter =
tree.children &&
tree.children.length > 0 &&
(tree.children
.map((child) => filterTree(child))
.filter((child) => child !== undefined) as FileTreeProps[]);
const compareQueryAndName =
tree.name.toLowerCase().includes(query.toLowerCase()) ||
query.toLowerCase().includes(tree.name.toLowerCase());
if (tree.isMat && compareQueryAndName) {
return tree;
}
if (!tree.isMat && tree.name !== query && filter && filter.length > 0) {
return { name: tree.name, children: filter };
}
return undefined;
}