github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/utils/compact-path.js (about) 1 /** 2 * Takes a branch created by our path-tree, and if it has only a single directory as descendent and no files, compacts it down to its terminal folder (the first descendent folder with either files or branching directories) 3 * Uses tail recursion 4 * @param {import("./path-tree").NestedPathTreeNode} branch 5 * @returns 6 */ 7 export default function compactPath(branch, name = '') { 8 let { children, files } = branch; 9 if (children && Object.keys(children).length === 1 && !files.length) { 10 const [key] = Object.keys(children); 11 const child = children[key]; 12 return compactPath(child, `${name}/${key}`); 13 } 14 return { 15 name, 16 data: branch, 17 }; 18 }