mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: add makeParent options to addChild
This commit is contained in:
1 parent
ba610a3fc5
commit
fd26be5e64
3 files changed
+44
-6
No files matched your search
@@ -1,15 +1,15 @@
|
|||||||
import path from 'path'
|
|
||||||
import File from './file.js'
|
import File from './file.js'
|
||||||
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
||||||
|
import { basename, dirname } from './utils.js'
|
||||||
|
|
||||||
export default class FileNode extends File {
|
export default class FileNode extends File {
|
||||||
constructor(file) {
|
constructor(file) {
|
||||||
super(file)
|
super(file)
|
||||||
this.children = []
|
this.children = file.children || []
|
||||||
}
|
}
|
||||||
|
|
||||||
addChild(fileInput) {
|
addChild(fileInput, { makeParents } = {}) {
|
||||||
const file = new File(fileInput)
|
const file = new FileNode(fileInput)
|
||||||
if (file.path === this.path) {
|
if (file.path === this.path) {
|
||||||
throw new DuplicateItemError('File already exists in the tree')
|
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')
|
throw new DuplicateItemError('File already exists in the tree')
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentPath = path.dirname(file.path)
|
const parentPath = dirname(file.path)
|
||||||
if (parentPath === this.path) {
|
if (parentPath === this.path) {
|
||||||
// Direct child of this node
|
// Direct child of this node
|
||||||
this.children.push(new FileNode(file))
|
this.children.push(file)
|
||||||
} else {
|
} else {
|
||||||
// Recursively call addChild on the correct child node
|
// Recursively call addChild on the correct child node
|
||||||
const childNode = this.children.find((child) => child.contains(file))
|
const childNode = this.children.find((child) => child.contains(file))
|
||||||
if (childNode) {
|
if (childNode) {
|
||||||
childNode.addChild(file)
|
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 {
|
} else {
|
||||||
throw new ParentNotExistsError('Parent path not found in the tree')
|
throw new ParentNotExistsError('Parent path not found in the tree')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,4 +74,23 @@ describe('FileNode', () => {
|
|||||||
expect(rootNode.children[1].children.length).toBe(1)
|
expect(rootNode.children[1].children.length).toBe(1)
|
||||||
console.log(rootNode.toString())
|
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