Skip to content

Commit fecafb8

Browse files
committed
feat: find-by for tree-utils
1 parent 55a89fe commit fecafb8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/lib/find-by.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ interface Options {
33
children?: string;
44
}
55

6+
type FindCallback = (item: any, index: number, array: any[]) => boolean;
7+
68
const defaults: Options = {
79
value: 'value',
810
children: 'children',
911
};
1012

11-
const findBy = (inTree: any, inCallback: (item: any) => boolean, inOptions?: Options) => {
13+
const findBy = (inTree: any, inCallback: FindCallback, inOptions?: Options) => {
1214
const result: any[] = [];
1315
const options = { ...defaults, ...inOptions };
1416
if (!inTree) return null;
1517

1618
const tree = Array.isArray(inTree) ? inTree : [inTree];
1719

1820
for (const item of tree) {
19-
if (inCallback(item)) {
21+
if (inCallback(item, tree.indexOf(item), tree)) {
2022
result.push(item);
2123
}
2224

2325
if (item[options.children!]) {
24-
const children = find(item[options.children!], inCallback, options) as any[];
26+
const children = findBy(item[options.children!], inCallback, options) as any[];
2527
if (children.length) {
2628
result.push(...children);
2729
}

0 commit comments

Comments
 (0)