File tree 1 file changed +5
-3
lines changed
1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change @@ -3,25 +3,27 @@ interface Options {
3
3
children ?: string ;
4
4
}
5
5
6
+ type FindCallback = ( item : any , index : number , array : any [ ] ) => boolean ;
7
+
6
8
const defaults : Options = {
7
9
value : 'value' ,
8
10
children : 'children' ,
9
11
} ;
10
12
11
- const findBy = ( inTree : any , inCallback : ( item : any ) => boolean , inOptions ?: Options ) => {
13
+ const findBy = ( inTree : any , inCallback : FindCallback , inOptions ?: Options ) => {
12
14
const result : any [ ] = [ ] ;
13
15
const options = { ...defaults , ...inOptions } ;
14
16
if ( ! inTree ) return null ;
15
17
16
18
const tree = Array . isArray ( inTree ) ? inTree : [ inTree ] ;
17
19
18
20
for ( const item of tree ) {
19
- if ( inCallback ( item ) ) {
21
+ if ( inCallback ( item , tree . indexOf ( item ) , tree ) ) {
20
22
result . push ( item ) ;
21
23
}
22
24
23
25
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 [ ] ;
25
27
if ( children . length ) {
26
28
result . push ( ...children ) ;
27
29
}
You can’t perform that action at this time.
0 commit comments