Skip to content

Commit 44cfc63

Browse files
committed
fix(tree): correct right parameter of level
1 parent 43849a3 commit 44cfc63

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/tree.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ export function forEachDeep<V>(
2626
children: string = 'children',
2727
isReverse = false
2828
): void {
29-
let level = 0,
30-
isBreak = false;
31-
const walk = (arr: ArrayLike<V>, parent: V | null) => {
29+
let isBreak = false;
30+
const walk = (arr: ArrayLike<V>, parent: V | null, level = 0) => {
3231
if (isReverse) {
3332
for (let i = arr.length - 1; i >= 0; i--) {
3433
if (isBreak) {
@@ -43,9 +42,8 @@ export function forEachDeep<V>(
4342
}
4443
// @ts-ignore
4544
if (arr[i] && Array.isArray(arr[i][children])) {
46-
++level;
4745
// @ts-ignore
48-
walk(arr[i][children], arr[i]);
46+
walk(arr[i][children], arr[i], level + 1);
4947
}
5048
}
5149
} else {
@@ -62,9 +60,8 @@ export function forEachDeep<V>(
6260
}
6361
// @ts-ignore
6462
if (arr[i] && Array.isArray(arr[i][children])) {
65-
++level;
6663
// @ts-ignore
67-
walk(arr[i][children], arr[i]);
64+
walk(arr[i][children], arr[i], level + 1);
6865
}
6966
}
7067
}

test/tree.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ test('forEachDeep', () => {
6666
const res3: string[] = [];
6767
const res4: string[] = [];
6868

69-
forEachDeep(tree, ({ id, name }) => {
69+
forEachDeep(tree, ({ id, name }, i, currentArr, tree, parent, level) => {
7070
res1.push(name);
71+
console.log('level', level);
7172
});
7273
expect(res1).toEqual(['row1', 'row2', 'row2-1', 'row3']);
7374

0 commit comments

Comments
 (0)