diff --git a/npm/ng-packs/packages/core/src/lib/tests/tree-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/tree-utils.spec.ts index 454c1c6891..6115114872 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/tree-utils.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/tree-utils.spec.ts @@ -1,4 +1,4 @@ -import { createTreeFromList, TreeNode } from '../utils/tree-utils'; +import { BaseTreeNode, createTreeFromList, TreeNode } from '../utils/tree-utils'; const LIST_1 = [ { id: 1, pid: null }, @@ -46,23 +46,32 @@ describe('Tree Utils', () => { ${LIST_2} | ${TREE_2} ${LIST_3} | ${TREE_3} `('should return $expected when given $list', ({ list, expected }: TestCreateTreeFromList) => { - expect( - createTreeFromList( - list, - x => x.id, - x => x.pid, - ), - ).toEqual(expected); + const tree = createTreeFromList( + list, + x => x.id, + x => x.pid, + x => BaseTreeNode.create(x), + ); + + expect(removeParents(tree)).toEqual(expected); }); }); }); +function removeParents(tree: TreeNode[]) { + return tree.map(v => { + const { parent, ...node } = v; + node.children = removeParents(node.children); + return node; + }); +} + interface TestCreateTreeFromList { - list: ModelA[]; - expected: TreeNode[]; + list: Model[]; + expected: TreeNode[]; } -interface ModelA { +interface Model { id: 1; pid: null; } diff --git a/npm/ng-packs/packages/core/src/lib/utils/tree-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/tree-utils.ts index 607a30ec52..d4c0ba0ab3 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/tree-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/tree-utils.ts @@ -1,19 +1,20 @@ -export class TreeNodeFactory { +export class BaseTreeNode { children: TreeNode[] = []; isLeaf = true; + parent: TreeNode; constructor(props: T) { Object.assign(this, props); } static create(props: T) { - return new TreeNodeFactory(props) as TreeNode; + return new BaseTreeNode(props) as TreeNode; } } export function createTreeFromList( list: T[], - keySelector: (item: T) => number | string | Symbol, + keySelector: (item: T) => number | string | symbol, parentKeySelector: typeof keySelector, valueMapper: (item: T) => R, ) { @@ -27,8 +28,10 @@ export function createTreeFromList( if (parentId) { const parent = map.get(parentId); + if (!parent) return; (parent as any).children.push(node); (parent as any).isLeaf = false; + (node as any).parent = parent; } else { tree.push(node); } @@ -39,7 +42,7 @@ export function createTreeFromList( export function createMapFromList( list: T[], - keySelector: (item: T) => number | string | Symbol, + keySelector: (item: T) => number | string | symbol, valueMapper: (item: T) => R, ) { type Key = ReturnType; @@ -54,6 +57,7 @@ export type TreeNode = { } & { children: TreeNode[]; isLeaf: boolean; + parent?: TreeNode; }; type NodeValue any> = F extends undefined