Skip to content

Commit 0e9cf21

Browse files
committed
feat: walkFlatDataGenerator stat add id, pid
1 parent e097cdf commit 0e9cf21

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/HeTree.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ const flatDataDefaultOptions = {
851851
parentIdKey: 'parent_id',
852852
}
853853
export type WalkFlatDataYield<T> = [T, {
854-
parent: T | null, parents: T[], index: number, treeIndex: number, skipChildren: VoidFunction, exitWalk: VoidFunction
854+
parent: T | null, parents: T[], index: number, treeIndex: number, id: Id, pid: Id, skipChildren: VoidFunction, exitWalk: VoidFunction
855855
}]
856856
export function* walkFlatDataGenerator<T extends Record<Id, any>>(flatData: T[], options0?: Partial<typeof flatDataDefaultOptions>): Generator<WalkFlatDataYield<T>> {
857857
const options = { ...flatDataDefaultOptions, ...options0 }
@@ -879,6 +879,8 @@ export function* walkFlatDataGenerator<T extends Record<Id, any>>(flatData: T[],
879879
parent,
880880
parents: parent ? [...stats[pid]!.parents, parent] : [],
881881
index,
882+
id,
883+
pid,
882884
treeIndex,
883885
skipChildren,
884886
exitWalk,

src/test/flatData.test.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,30 @@ test("walkFlatDataGenerator: exitWalk", () => {
124124
});
125125
test("walkFlatData", () => {
126126
let data = createData();
127-
walkFlatData(data, (node, { treeIndex, parent, index, parents }) => {
128-
if (treeIndex === 0) {
129-
expect(parent).toBe(null);
130-
expect(JSON.stringify(parents)).toBe("[]");
131-
expect(node.id).toBe(1);
127+
walkFlatData(
128+
data,
129+
(node, { treeIndex, parent, index, parents, exitWalk }) => {
130+
if (treeIndex === 0) {
131+
expect(parent).toBe(null);
132+
expect(JSON.stringify(parents)).toBe("[]");
133+
expect(node.id).toBe(1);
134+
exitWalk();
135+
}
132136
}
133-
});
137+
);
138+
});
139+
test("walkFlatData: id, pid", () => {
140+
let data = createData();
141+
walkFlatData(
142+
data,
143+
(node, { treeIndex, parent, index, parents, exitWalk, id, pid }) => {
144+
if (treeIndex === 2) {
145+
expect(id).toBe(5);
146+
expect(pid).toBe(2);
147+
exitWalk();
148+
}
149+
}
150+
);
134151
});
135152
test("walkFlatData: options", () => {
136153
let data = createData("key", "pid");

0 commit comments

Comments
 (0)