tree
操作树结构数据的工具函数,支持树结构转换、查找、过滤、遍历和节点操作等功能。
@movk-repo/shared/utils/tree
类型定义
ts
interface TreeConfig {
id: string
children: string
pid: string
}
interface TreeNode {
[key: string]: any
}
fromList
将扁平数组转换为嵌套树结构。
ts
function fromList(list: TreeNode[], config?: Partial<TreeConfig>): TreeNode[]
ts
import { fromList } from '@movk-repo/shared'
const list = [
{ id: 1, name: 'Node 1', pid: 0 },
{ id: 2, name: 'Node 2', pid: 1 }
]
const tree = fromList(list)
// 返回: [{ id: 1, name: 'Node 1', pid: 0, children: [{ id: 2, name: 'Node 2', pid: 1, children: [] }] }]
toList
将树结构转换为扁平数组,子节点跟随在其父节点后面。
ts
function toList(tree: TreeNode[], config?: Partial<TreeConfig>): TreeNode[]
ts
import { toList } from '@movk-repo/shared'
const tree = [{
id: 1,
name: 'Node 1',
children: [
{ id: 2, name: 'Node 2', children: [] }
]
}]
const list = toList(tree)
// 返回: [{ id: 1, name: 'Node 1', children: [...] }, { id: 2, name: 'Node 2', children: [] }]
findNode
查找满足条件的第一个节点。
ts
function findNode(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): TreeNode | null
ts
import { findNode } from '@movk-repo/shared'
const tree = [{ id: 1, name: 'Node 1', children: [{ id: 2, name: 'Node 2' }] }]
const node = findNode(tree, node => node.id === 2)
// 返回: { id: 2, name: 'Node 2' }
findNodeAll
查找所有满足条件的节点。
ts
function findNodeAll(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): TreeNode[]
ts
import { findNodeAll } from '@movk-repo/shared'
const tree = [{ id: 1, type: 'folder', children: [{ id: 2, type: 'file' }] }]
const nodes = findNodeAll(tree, node => node.type === 'file')
// 返回: [{ id: 2, type: 'file' }]
findPath
查找满足条件的节点的路径。
ts
function findPath(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): TreeNode[] | null
ts
import { findPath } from '@movk-repo/shared'
const tree = [{ id: 1, name: 'Root', children: [{ id: 2, name: 'Child' }] }]
const path = findPath(tree, node => node.id === 2)
// 返回: [{ id: 1, name: 'Root', children: [...] }, { id: 2, name: 'Child' }]
findPathAll
查找所有满足条件的节点的路径。
ts
function findPathAll(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): TreeNode[][]
ts
import { findPathAll } from '@movk-repo/shared'
const tree = [{ id: 1, type: 'folder', children: [{ id: 2, type: 'file' }] }]
const paths = findPathAll(tree, node => node.type === 'file')
// 返回: [[{ id: 1, type: 'folder', children: [...] }, { id: 2, type: 'file' }]]
filterTree
过滤树,返回满足条件的节点及其所有父节点。
ts
function filterTree(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): TreeNode[]
ts
import { filterTree } from '@movk-repo/shared'
const tree = [{ id: 1, name: 'Root', children: [{ id: 2, name: 'Target' }] }]
const filtered = filterTree(tree, node => node.name.includes('Target'))
// 返回: [{ id: 1, name: 'Root', children: [{ id: 2, name: 'Target', children: [] }] }]
forEachTreeNode
遍历树中的每个节点。
ts
function forEachTreeNode(
tree: TreeNode[],
func: (node: TreeNode) => void,
config?: Partial<TreeConfig>
): void
ts
import { forEachTreeNode } from '@movk-repo/shared'
const tree = [{ id: 1, children: [{ id: 2 }] }]
forEachTreeNode(tree, node => console.log(node.id))
// 输出:1, 2
insertBefore
在指定节点之前插入新节点。
ts
function insertBefore(
tree: TreeNode[],
oldNode: TreeNode,
newNode: TreeNode,
config?: Partial<TreeConfig>
): void
ts
import { insertBefore } from '@movk-repo/shared'
const tree = [{ id: 1 }]
const node = { id: 2 }
insertBefore(tree, tree[0], node)
// tree 变为: [{ id: 2 }, { id: 1 }]
insertAfter
在指定节点之后插入新节点。
ts
function insertAfter(
tree: TreeNode[],
oldNode: TreeNode,
newNode: TreeNode,
config?: Partial<TreeConfig>
): void
ts
import { insertAfter } from '@movk-repo/shared'
const tree = [{ id: 1 }]
const node = { id: 2 }
insertAfter(tree, tree[0], node)
// tree 变为: [{ id: 1 }, { id: 2 }]
removeNode
移除树中满足条件的节点。
ts
function removeNode(
tree: TreeNode[],
func: (node: TreeNode) => boolean,
config?: Partial<TreeConfig>
): void
ts
import { removeNode } from '@movk-repo/shared'
const tree = [{ id: 1, children: [{ id: 2 }] }]
removeNode(tree, node => node.id === 2)
// tree 变为: [{ id: 1, children: [] }]