Enable ts-strict for treeUtil.ts (#1238)

This commit is contained in:
Chenlei Hu
2024-10-12 14:10:08 -04:00
committed by GitHub
parent 634196cbd6
commit dfcabd2834

View File

@@ -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',
@@ -16,7 +12,7 @@ export function buildTree<T>(
}
for (const item of items) {
const keys = typeof key === 'string' ? item[key] : key(item)
const keys = key(item)
let parent = root
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
@@ -33,7 +29,7 @@ export function buildTree<T>(
children: []
}
map[id] = node
parent.children.push(node)
parent.children?.push(node)
}
parent = map[id]
}
@@ -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 = []