Skip to content

Commit 7f3f33f

Browse files
committed
fix: improve params naming
1 parent 83fd376 commit 7f3f33f

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

src/tree.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface IFilterCondition<V> {
3131
* @param {ArrayLike<V>} tree 树形数据
3232
* @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
3333
* @param {options} options 支持定制子元素名称、反向遍历、广度优先遍历,默认{
34-
children: 'children',
34+
childField: 'children',
3535
reverse: false,
3636
breadthFirst: false
3737
}
@@ -47,17 +47,22 @@ export function forEachDeep<V>(
4747
parent: V | null,
4848
level: number
4949
) => boolean | void,
50-
51-
options: { children?: string; reverse?: boolean; breadthFirst?: boolean } = {
52-
children: 'children',
50+
options: { childField?: string; reverse?: boolean; breadthFirst?: boolean } = {
51+
childField: 'children',
5352
reverse: false,
5453
breadthFirst: false
5554
}
5655
): void {
57-
const { children = 'children', reverse = false, breadthFirst = false } = isObject(options) ? options : {};
56+
const { childField = 'children', reverse = false, breadthFirst = false } = isObject(options) ? options : {};
5857
let isBreak = false;
59-
const queue: { item: V; index: number; array: ArrayLike<V>; tree: ArrayLike<V>; parent: V | null; level: number }[] =
60-
[];
58+
const queue: {
59+
item: V;
60+
index: number;
61+
array: ArrayLike<V>;
62+
tree: ArrayLike<V>;
63+
parent: V | null;
64+
level: number;
65+
}[] = [];
6166
const walk = (arr: ArrayLike<V>, parent: V | null, level = 0) => {
6267
if (reverse) {
6368
for (let index = arr.length - 1; index >= 0; index--) {
@@ -67,7 +72,7 @@ export function forEachDeep<V>(
6772
const item = arr[index];
6873
// 广度优先
6974
if (breadthFirst) {
70-
queue.push({ item, index: index, array: arr, tree, parent, level });
75+
queue.push({ item, index, array: arr, tree, parent, level });
7176
} else {
7277
const re = iterator(item, index, arr, tree, parent, level);
7378
if (re === false) {
@@ -77,13 +82,14 @@ export function forEachDeep<V>(
7782
continue;
7883
}
7984
// @ts-ignore
80-
if (item && Array.isArray(item[children])) {
85+
if (item && Array.isArray(item[childField])) {
8186
// @ts-ignore
82-
walk(item[children], item, level + 1);
87+
walk(item[childField], item, level + 1);
8388
}
8489
}
8590
}
8691
if (breadthFirst) {
92+
// Process queue
8793
while (!isBreak) {
8894
const current = queue.shift();
8995

@@ -99,9 +105,9 @@ export function forEachDeep<V>(
99105
}
100106

101107
// @ts-ignore
102-
if (item && Array.isArray(item[children])) {
108+
if (item && Array.isArray(item[childField])) {
103109
// @ts-ignore
104-
walk(item[children], item, level + 1);
110+
walk(item[childField], item, level + 1);
105111
}
106112
}
107113
}
@@ -125,9 +131,9 @@ export function forEachDeep<V>(
125131
continue;
126132
}
127133
// @ts-ignore
128-
if (item && Array.isArray(item[children])) {
134+
if (item && Array.isArray(item[childField])) {
129135
// @ts-ignore
130-
walk(item[children], item, level + 1);
136+
walk(item[childField], item, level + 1);
131137
}
132138
}
133139
}
@@ -146,15 +152,15 @@ export function forEachDeep<V>(
146152
}
147153

148154
// @ts-ignore
149-
if (item && Array.isArray(item[children])) {
155+
if (item && Array.isArray(item[childField])) {
150156
// @ts-ignore
151-
walk(item[children], item, level + 1);
157+
walk(item[childField], item, level + 1);
152158
}
153159
}
154160
}
155161
}
156162
};
157-
walk(tree, null);
163+
walk(tree, null, 0);
158164
// @ts-ignore
159165
tree = null;
160166
}
@@ -352,7 +358,12 @@ export function flatTree(treeList: any[], options: IFieldOptions = defaultFieldO
352358
* 2. 若无任何过滤条件或keyword模式匹配且keyword为空串,返回原对象;其他情况返回新数组
353359
* @param {V[]} nodes
354360
* @param {IFilterCondition} filterCondition
355-
* @param {ISearchTreeOpts} options
361+
* @param {ISearchTreeOpts} options 默认配置项 {
362+
childField: 'children',
363+
nameField: 'name',
364+
removeEmptyChild: false,
365+
ignoreCase: true
366+
}
356367
* @returns {V[]}
357368
*/
358369
export function fuzzySearchTree<V>(

test/tree.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ test('forEachDeep', () => {
7575
{ id: 5, name: 'row5' }
7676
];
7777

78+
const tree22 = [
79+
{ id: 1, name: 'row1' },
80+
{
81+
id: 2,
82+
name: 'row2',
83+
child: [{ id: 21, name: 'row2-1' }]
84+
},
85+
{ id: 3, name: 'row3' },
86+
{ id: 4, name: 'row4' },
87+
{ id: 5, name: 'row5' }
88+
];
89+
7890
const tree3 = [
7991
{ id: 1, name: 'row1' },
8092
{
@@ -121,10 +133,21 @@ test('forEachDeep', () => {
121133
const res2: string[] = [];
122134
const res3: string[] = [];
123135
const res4: string[] = [];
124-
const breadthRes1: string[] = [];
136+
let breadthRes1: string[] = [];
125137
const breadthRes2: string[] = [];
126138
const breadthRes3: string[] = [];
127139

140+
forEachDeep(
141+
tree22,
142+
({ id, name }, i, currentArr, tree, parent, level) => {
143+
breadthRes1.push(name);
144+
// console.log('level', level);
145+
},
146+
{ breadthFirst: true, childField: 'child' }
147+
);
148+
expect(breadthRes1).toEqual(['row1', 'row2', 'row3', 'row4', 'row5', 'row2-1']);
149+
150+
breadthRes1 = [];
128151
forEachDeep(
129152
tree2,
130153
({ id, name }, i, currentArr, tree, parent, level) => {
@@ -159,7 +182,6 @@ test('forEachDeep', () => {
159182
},
160183
{ breadthFirst: true }
161184
);
162-
console.log('breadthRes3', breadthRes3);
163185
expect(breadthRes3).toEqual([
164186
'row1',
165187
'row2',

0 commit comments

Comments
 (0)