open-nomad/ui/app/utils/compact-path.js
Phil Renaud 4c58356af1 Path Tree compaction refactor (#13415)
* Bones of a just-in-time compaction pathTree

* wooo got compaction going in sub-ms times

* PR cleanup

* Path compaction tests

* lint fix to equal instead of .ok()

* Name prop specifically being equality checked
2022-07-11 13:34:05 -04:00

19 lines
665 B
JavaScript

/**
* 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)
* Uses tail recursion
* @param {import("./path-tree").NestedPathTreeNode} branch
* @returns
*/
export default function compactPath(branch, name = '') {
let { children, files } = branch;
if (children && Object.keys(children).length === 1 && !files.length) {
const [key] = Object.keys(children);
const child = children[key];
return compactPath(child, `${name}/${key}`);
}
return {
name,
data: branch,
};
}