Skip to content

Commit

Permalink
Merge pull request #7363 from abpframework/feat/add-create-tree-node-…
Browse files Browse the repository at this point in the history
…filter-creator

Added createTreeNodeFilterCreator utility function
  • Loading branch information
mehmet-erim committed Jan 21, 2021
2 parents f5c3f30 + 5fc956e commit e79c8fb
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
75 changes: 72 additions & 3 deletions npm/ng-packs/packages/core/src/lib/tests/tree-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BaseTreeNode, createTreeFromList, TreeNode } from '../utils/tree-utils';
import {
BaseTreeNode,
createTreeFromList,
createTreeNodeFilterCreator,
TreeNode,
} from '../utils/tree-utils';

const LIST_1 = [
{ id: 1, pid: null },
Expand Down Expand Up @@ -38,6 +43,42 @@ const TREE_3 = [
],
},
];
const SOURCE_TREE: TreeNode<SearchModel>[] = [
{
id: 1,
pid: null,
isLeaf: false,
name: 'foo',
children: [
{
id: 2,
pid: 1,
name: 'bar',
isLeaf: false,
children: [{ id: 3, pid: 2, name: 'qux', isLeaf: true, children: [] }],
},
{ id: 4, pid: 1, name: 'baz', isLeaf: true, children: [] },
{ id: 5, pid: 1, name: 'quux', isLeaf: true, children: [] },
],
},
];
const RESULT_TREE_1 = [
{ id: 3, pid: 2, name: 'qux', isLeaf: true, children: [] },
{ id: 5, pid: 1, name: 'quux', isLeaf: true, children: [] },
];
const RESULT_TREE_2 = [{ id: 5, pid: 1, name: 'quux', isLeaf: true, children: [] }];
const RESULT_TREE_3 = [
{
id: 2,
pid: 1,
name: 'bar',
isLeaf: false,
children: [{ id: 3, pid: 2, name: 'qux', isLeaf: true, children: [] }],
},
{ id: 4, pid: 1, name: 'baz', isLeaf: true, children: [] },
];
const RESULT_TREE_4 = [{ id: 4, pid: 1, name: 'baz', isLeaf: true, children: [] }];

describe('Tree Utils', () => {
describe('createTreeFromList', () => {
test.each`
Expand All @@ -56,6 +97,23 @@ describe('Tree Utils', () => {
expect(removeParents(tree)).toEqual(expected);
});
});

describe('createTreeNodeFilterCreator', () => {
test.each`
search | expected
${'qu'} | ${RESULT_TREE_1}
${'quu'} | ${RESULT_TREE_2}
${'ba'} | ${RESULT_TREE_3}
${'baz'} | ${RESULT_TREE_4}
`(
'should return $expected when $search is searched',
({ search, expected }: TestCreateTreeNodeFilter) => {
const filter = createTreeNodeFilterCreator('name', String)(search);

expect(filter(SOURCE_TREE)).toEqual(expected);
},
);
});
});

function removeParents(tree: TreeNode<Model>[]) {
Expand All @@ -72,6 +130,17 @@ interface TestCreateTreeFromList {
}

interface Model {
id: 1;
pid: null;
id: number;
pid?: number;
}

interface TestCreateTreeNodeFilter {
search: string;
expected: TreeNode<SearchModel>[];
}

interface SearchModel {
id: number;
pid?: number;
name: string;
}
19 changes: 19 additions & 0 deletions npm/ng-packs/packages/core/src/lib/utils/tree-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ export function createMapFromList<T extends object, R extends unknown>(
return map;
}

export function createTreeNodeFilterCreator<T extends object>(
key: keyof T,
mapperFn: (value: any) => string,
) {
return (search: string) => {
const regex = new RegExp('.*' + search + '.*', 'i');

return function collectNodes(nodes: TreeNode<T>[], matches = []) {
for (const node of nodes) {
if (regex.test(mapperFn(node[key]))) matches.push(node);

if (node.children.length) collectNodes(node.children, matches);
}

return matches;
};
};
}

export type TreeNode<T extends object> = {
[K in keyof T]: T[K];
} & {
Expand Down

0 comments on commit e79c8fb

Please sign in to comment.