mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: create FileNode class, addChild method
This commit is contained in:
1 parent
bdb979e608
commit
fa496ceae7
4 files changed
+131
No files matched your search
@@ -0,0 +1,16 @@
|
||||
export default class File {
|
||||
constructor({ path, name, type, discovered }) {
|
||||
this.path = path
|
||||
this.name = name
|
||||
this.type = type
|
||||
this.discovered = discovered
|
||||
}
|
||||
|
||||
isDirectory() {
|
||||
return this.type === 'folder'
|
||||
}
|
||||
|
||||
contains(file) {
|
||||
return this.isDirectory() && file.path.startsWith(this.path)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import path from 'path'
|
||||
import File from './file.js'
|
||||
|
||||
export default class FileNode extends File {
|
||||
constructor(file) {
|
||||
super(file)
|
||||
this.children = []
|
||||
}
|
||||
|
||||
addChild(fileInput) {
|
||||
const file = new File(fileInput)
|
||||
if (file.path === this.path) {
|
||||
throw new Error('File already exists in the tree')
|
||||
}
|
||||
|
||||
const parentPath = path.dirname(file.path)
|
||||
if (parentPath === this.path) {
|
||||
// Direct child of this node
|
||||
this.children.push(new FileNode(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 {
|
||||
console.log(fileInput, this)
|
||||
throw new Error('Parent path not found in the tree')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
const indent = ' '.repeat(this.path.split('/').length - 1)
|
||||
const childrenString = this.children
|
||||
.map((child) => child.toString())
|
||||
.join('')
|
||||
return `${indent}${this.name}${
|
||||
this.isDirectory() ? '/' : ''
|
||||
}\n${childrenString}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import FileNode from './fileNode.js'
|
||||
|
||||
test('addChild method adds a child node to the tree', () => {
|
||||
const files = [
|
||||
{
|
||||
path: '/home/rudeus',
|
||||
name: 'rudeus',
|
||||
type: 'folder',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.bash_history',
|
||||
name: '.bash_history',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.viminfo',
|
||||
name: '.viminfo',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/.zshrc',
|
||||
name: '.zshrc',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/forgotten_scroll.txt',
|
||||
name: 'forgotten_scroll.txt',
|
||||
type: 'file',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test',
|
||||
name: 'test',
|
||||
type: 'folder',
|
||||
discovered: 'test',
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test/.b',
|
||||
name: '.b',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
},
|
||||
{
|
||||
path: '/home/rudeus/test/a',
|
||||
name: 'a',
|
||||
type: 'file',
|
||||
discovered: false,
|
||||
},
|
||||
]
|
||||
|
||||
const root = new FileNode(files[0])
|
||||
|
||||
files.slice(1).forEach((file) => {
|
||||
root.addChild(file)
|
||||
})
|
||||
|
||||
console.log(root.toString())
|
||||
|
||||
expect(root.children.length).toBe(5)
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "file-graph",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user