feat
This commit is contained in:
@@ -42,16 +42,8 @@ export interface BuildTreeResult<T extends TreeNodeBase> {
|
||||
* @param options 构建选项
|
||||
* @returns 包含根节点列表和节点映射的对象
|
||||
*/
|
||||
export function buildTree<T extends TreeNodeBase>(
|
||||
items: T[],
|
||||
options: BuildTreeOptions<T> = {}
|
||||
): BuildTreeResult<T> {
|
||||
const {
|
||||
idKey = 'id' as keyof T,
|
||||
parentKey = 'parent_id' as keyof T,
|
||||
childrenKey = 'children',
|
||||
orderKey,
|
||||
} = options
|
||||
export function buildTree<T extends TreeNodeBase>(items: T[], options: BuildTreeOptions<T> = {}): BuildTreeResult<T> {
|
||||
const { idKey = 'id' as keyof T, parentKey = 'parent_id' as keyof T, childrenKey = 'children', orderKey } = options
|
||||
|
||||
const itemMap = new Map<number | string, T>()
|
||||
const rootItems: T[] = []
|
||||
@@ -61,10 +53,13 @@ export function buildTree<T extends TreeNodeBase>(
|
||||
const id = item[idKey]
|
||||
if (id !== undefined && id !== null) {
|
||||
// 创建带有空 children 数组的节点副本
|
||||
itemMap.set(id as number | string, {
|
||||
...item,
|
||||
[childrenKey]: []
|
||||
} as T)
|
||||
itemMap.set(
|
||||
id as number | string,
|
||||
{
|
||||
...item,
|
||||
[childrenKey]: [],
|
||||
} as T
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -164,7 +159,7 @@ export function getAncestors<T extends TreeNodeBase>(
|
||||
): T[] {
|
||||
const ancestors: T[] = []
|
||||
let current = itemMap.get(nodeId)
|
||||
|
||||
|
||||
while (current) {
|
||||
const parentId = current[parentKey]
|
||||
if (parentId && itemMap.has(parentId as number | string)) {
|
||||
@@ -175,7 +170,7 @@ export function getAncestors<T extends TreeNodeBase>(
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ancestors
|
||||
}
|
||||
|
||||
@@ -185,20 +180,17 @@ export function getAncestors<T extends TreeNodeBase>(
|
||||
* @param childrenKey 子节点字段名
|
||||
* @returns 所有子孙节点(扁平数组)
|
||||
*/
|
||||
export function getDescendants<T extends TreeNodeBase>(
|
||||
node: T,
|
||||
childrenKey: string = 'children'
|
||||
): T[] {
|
||||
export function getDescendants<T extends TreeNodeBase>(node: T, childrenKey: string = 'children'): T[] {
|
||||
const descendants: T[] = []
|
||||
const children = node[childrenKey] as T[]
|
||||
|
||||
|
||||
if (children && children.length > 0) {
|
||||
for (const child of children) {
|
||||
descendants.push(child)
|
||||
descendants.push(...getDescendants(child, childrenKey))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return descendants
|
||||
}
|
||||
|
||||
@@ -208,16 +200,17 @@ export function getDescendants<T extends TreeNodeBase>(
|
||||
* @param childrenKey 子节点字段名
|
||||
* @returns 扁平化后的数组(移除 children 属性)
|
||||
*/
|
||||
export function flattenTree<T extends TreeNodeBase>(
|
||||
nodes: T[],
|
||||
childrenKey: string = 'children'
|
||||
): Omit<T, 'children'>[] {
|
||||
export function flattenTree<T extends TreeNodeBase>(nodes: T[], childrenKey: string = 'children'): Omit<T, 'children'>[] {
|
||||
const result: Omit<T, 'children'>[] = []
|
||||
|
||||
traverseTree(nodes, (node) => {
|
||||
const { [childrenKey]: _, ...rest } = node
|
||||
result.push(rest as Omit<T, 'children'>)
|
||||
}, childrenKey)
|
||||
|
||||
|
||||
traverseTree(
|
||||
nodes,
|
||||
(node) => {
|
||||
const { [childrenKey]: _, ...rest } = node
|
||||
result.push(rest as Omit<T, 'children'>)
|
||||
},
|
||||
childrenKey
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user