mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: add makeParent options to addChild
This commit is contained in:
1 parent
781e01d43a
commit
1778780364
3 files changed
+44
-6
No files matched your search
@@ -1,15 +1,15 @@
|
||||
import path from 'path'
|
||||
import File from './file.js'
|
||||
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
||||
import { basename, dirname } from './utils.js'
|
||||
|
||||
export default class FileNode extends File {
|
||||
constructor(file) {
|
||||
super(file)
|
||||
this.children = []
|
||||
this.children = file.children || []
|
||||
}
|
||||
|
||||
addChild(fileInput) {
|
||||
const file = new File(fileInput)
|
||||
addChild(fileInput, { makeParents } = {}) {
|
||||
const file = new FileNode(fileInput)
|
||||
if (file.path === this.path) {
|
||||
throw new DuplicateItemError('File already exists in the tree')
|
||||
}
|
||||
@@ -18,15 +18,24 @@ export default class FileNode extends File {
|
||||
throw new DuplicateItemError('File already exists in the tree')
|
||||
}
|
||||
|
||||
const parentPath = path.dirname(file.path)
|
||||
const parentPath = dirname(file.path)
|
||||
if (parentPath === this.path) {
|
||||
// Direct child of this node
|
||||
this.children.push(new FileNode(file))
|
||||
this.children.push(file)
|
||||
} else {
|
||||
// Recursively call addChild on the correct child node
|
||||
const childNode = this.children.find((child) => child.contains(file))
|
||||
if (childNode) {
|
||||
childNode.addChild(file)
|
||||
} else if (makeParents) {
|
||||
const parent = new FileNode({
|
||||
path: parentPath,
|
||||
name: basename(parentPath),
|
||||
type: 'folder',
|
||||
discovered: false,
|
||||
children: [file],
|
||||
})
|
||||
this.addChild(parent, { makeParents })
|
||||
} else {
|
||||
throw new ParentNotExistsError('Parent path not found in the tree')
|
||||
}
|
||||
|
||||
@@ -74,4 +74,23 @@ describe('FileNode', () => {
|
||||
expect(rootNode.children[1].children.length).toBe(1)
|
||||
console.log(rootNode.toString())
|
||||
})
|
||||
|
||||
test('adds nested children to the tree with makeParents option', () => {
|
||||
const file = {
|
||||
path: '/home/rudeus/test/a/b/c/d/e',
|
||||
name: 'a',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
}
|
||||
|
||||
rootNode.addChild(file, { makeParents: true })
|
||||
|
||||
console.log(rootNode.toString())
|
||||
let currentNode = rootNode
|
||||
for (let i = 0; i < 5; i += 1) {
|
||||
expect(currentNode.children.length).toBe(1)
|
||||
// eslint-disable-next-line prefer-destructuring
|
||||
currentNode = currentNode.children[0]
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
export function dirname(path) {
|
||||
const parts = path.split('/')
|
||||
parts.pop()
|
||||
return parts.join('/')
|
||||
}
|
||||
|
||||
export function basename(path) {
|
||||
const parts = path.split('/')
|
||||
return parts.pop()
|
||||
}
|
||||
Reference in new issue
Block a user