feat: rewrite remove function and add remove test

This commit is contained in:
Chen, Tai-Lin committed 2023-08-28 16:13:10 +08:00
1 parent 887bbf5c1b
commit 7f2a57824b
3 files changed
+57 -35

No files matched your search

+20
View File
@@ -52,6 +52,26 @@ export default class FileNode extends File {
}
}
removeChild(fileToRemove) {
const file = new FileNode(fileToRemove)
const parentPath = dirname(file.path)
if (parentPath === this.path) {
// Direct child of this node
this.children = this.children.filter((child) => child.path !== file.path)
} else {
// Recursively call removeChild on the correct child node
const childNode = this.children.find((child) => child.contains(file))
if (childNode) {
childNode.removeChild(file)
} else {
throw new FileNotExistsError(
`File ${file.path} does not exist in the tree: ${this.toString()}`
)
}
}
}
merge(fileNode) {
// Merge the children of the provided file node into this node
// If a child already exists, merge the children of that child