mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: merge middle part
This commit is contained in:
1 parent
7405f786a0
commit
4255eb5910
2 files changed
+62
-19
No files matched your search
@@ -63,9 +63,11 @@ export default class FileNode extends File {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// If a child exists in this node but not in the provided node, remove it
|
// If a child exists in this node but not in the provided node, remove it
|
||||||
this.children = this.children.filter((child) =>
|
if (fileNode.children.length > 0) {
|
||||||
fileNode.children.some((c) => c.path === child.path)
|
this.children = this.children.filter((child) =>
|
||||||
)
|
fileNode.children.some((c) => c.path === child.path)
|
||||||
|
)
|
||||||
|
}
|
||||||
} else if (this.contains(fileNode)) {
|
} else if (this.contains(fileNode)) {
|
||||||
const childNode = this.children.find((child) => child.contains(fileNode))
|
const childNode = this.children.find((child) => child.contains(fileNode))
|
||||||
if (childNode) {
|
if (childNode) {
|
||||||
|
|||||||
@@ -1,6 +1,28 @@
|
|||||||
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
import { DuplicateItemError, ParentNotExistsError } from './errors.js'
|
||||||
import FileNode from './fileNode.js'
|
import FileNode from './fileNode.js'
|
||||||
|
|
||||||
|
function createNodeFromFiles(files) {
|
||||||
|
const node = new FileNode(files[0])
|
||||||
|
files.slice(1).forEach((file) => node.addChild(file))
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeTest(files1, files2, expectedFiles) {
|
||||||
|
const node1 = createNodeFromFiles(files1)
|
||||||
|
const node2 = createNodeFromFiles(files2)
|
||||||
|
const expectedNode = createNodeFromFiles(expectedFiles)
|
||||||
|
|
||||||
|
console.log('node1', node1.toString())
|
||||||
|
console.log('node2', node2.toString())
|
||||||
|
console.log('expected', expectedNode.toString())
|
||||||
|
|
||||||
|
node1.merge(node2)
|
||||||
|
|
||||||
|
console.log('after-merged', node1.toString())
|
||||||
|
|
||||||
|
expect(node1.toString()).toBe(expectedNode.toString())
|
||||||
|
}
|
||||||
|
|
||||||
describe('FileNode', () => {
|
describe('FileNode', () => {
|
||||||
let rootNode
|
let rootNode
|
||||||
|
|
||||||
@@ -94,12 +116,13 @@ describe('FileNode', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('merge-two-trees', () => {
|
test('merge-subtree', () => {
|
||||||
const root = {
|
const root = {
|
||||||
path: '/',
|
path: '/',
|
||||||
type: 'folder',
|
type: 'folder',
|
||||||
}
|
}
|
||||||
const files1 = [
|
const files1 = [
|
||||||
|
root,
|
||||||
{ path: '/a', type: 'file' },
|
{ path: '/a', type: 'file' },
|
||||||
{ path: '/b', type: 'folder' },
|
{ path: '/b', type: 'folder' },
|
||||||
{ path: '/b/c', type: 'folder' },
|
{ path: '/b/c', type: 'folder' },
|
||||||
@@ -115,6 +138,7 @@ describe('FileNode', () => {
|
|||||||
]
|
]
|
||||||
|
|
||||||
const expected = [
|
const expected = [
|
||||||
|
root,
|
||||||
{ path: '/a', type: 'file' },
|
{ path: '/a', type: 'file' },
|
||||||
{ path: '/b', type: 'folder' },
|
{ path: '/b', type: 'folder' },
|
||||||
{ path: '/b/c', type: 'folder' },
|
{ path: '/b/c', type: 'folder' },
|
||||||
@@ -122,24 +146,41 @@ describe('FileNode', () => {
|
|||||||
{ path: '/b/c/g', type: 'file' },
|
{ path: '/b/c/g', type: 'file' },
|
||||||
]
|
]
|
||||||
|
|
||||||
const node1 = new FileNode(root)
|
mergeTest(files1, files2, expected)
|
||||||
files1.forEach((file) => node1.addChild(file))
|
})
|
||||||
|
|
||||||
const node2 = new FileNode(files2[0])
|
test('merge-middle-level', () => {
|
||||||
files2.slice(1).forEach((file) => node2.addChild(file))
|
// Test if the node to merge is only the middle part of the tree
|
||||||
|
// (i.e. the node to merge doesn't contains all level of children)
|
||||||
|
// The original tree should keep the children that are not in the node to merge
|
||||||
|
const root = {
|
||||||
|
path: '/',
|
||||||
|
type: 'folder',
|
||||||
|
}
|
||||||
|
const files1 = [
|
||||||
|
root,
|
||||||
|
{ path: '/a', type: 'file' },
|
||||||
|
{ path: '/b', type: 'folder' },
|
||||||
|
{ path: '/b/c', type: 'folder' },
|
||||||
|
{ path: '/b/d', type: 'file' },
|
||||||
|
{ path: '/b/c/f', type: 'file' },
|
||||||
|
]
|
||||||
|
|
||||||
console.log('node1', node1.toString())
|
const files2 = [
|
||||||
console.log('node2', node2.toString())
|
{ path: '/b', type: 'folder' },
|
||||||
|
{ path: '/b/c', type: 'folder' },
|
||||||
|
{ path: '/b/e', type: 'file' },
|
||||||
|
]
|
||||||
|
|
||||||
const expectedNode = new FileNode(root)
|
const expected = [
|
||||||
expected.forEach((file) => expectedNode.addChild(file))
|
root,
|
||||||
|
{ path: '/a', type: 'file' },
|
||||||
|
{ path: '/b', type: 'folder' },
|
||||||
|
{ path: '/b/c', type: 'folder' },
|
||||||
|
{ path: '/b/e', type: 'file' },
|
||||||
|
{ path: '/b/c/f', type: 'file' },
|
||||||
|
]
|
||||||
|
|
||||||
console.log('expected', expectedNode.toString())
|
mergeTest(files1, files2, expected)
|
||||||
|
|
||||||
node1.merge(node2)
|
|
||||||
|
|
||||||
console.log('node1-after', node1.toString())
|
|
||||||
|
|
||||||
expect(node1.toString()).toBe(expectedNode.toString())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user