Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/utils/treeUtil.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// @ts-strict-ignore
import type { TreeNode } from 'primevue/treenode'

export function buildTree<T>(
items: T[],
key: string | ((item: T) => string[])
): TreeNode {
export function buildTree<T>(items: T[], key: (item: T) => string[]): TreeNode {
const root: TreeNode = {
key: 'root',
label: 'root',
Expand All @@ -16,7 +12,7 @@ export function buildTree<T>(
}

for (const item of items) {
const keys = typeof key === 'string' ? item[key] : key(item)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The item attr key is unused. Removing the support for it.

const keys = key(item)
let parent = root
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
Expand All @@ -33,7 +29,7 @@ export function buildTree<T>(
children: []
}
map[id] = node
parent.children.push(node)
parent.children?.push(node)
}
parent = map[id]
}
Expand Down Expand Up @@ -63,7 +59,7 @@ export function sortedTree(node: TreeNode): TreeNode {
if (node.children) {
// Sort the children of the current node
const sortedChildren = [...node.children].sort((a, b) =>
a.label.localeCompare(b.label)
(a.label ?? '').localeCompare(b.label ?? '')
)
// Recursively sort the children and add them to the new node
newNode.children = []
Expand Down