@@ -7,15 +7,16 @@ export class FilterBuilder {
77 const { and, or } = filter ;
88 const filters : FilterFn < DTO > [ ] = [ ] ;
99
10- if ( and ) {
10+ if ( and && and . length ) {
1111 filters . push ( this . andFilterFn ( ...and . map ( ( f ) => this . build ( f ) ) ) ) ;
1212 }
1313
14- if ( or ) {
14+ if ( or && or . length ) {
1515 filters . push ( this . orFilterFn ( ...or . map ( ( f ) => this . build ( f ) ) ) ) ;
1616 }
17-
18- filters . push ( this . filterFieldsOrNested ( filter ) ) ;
17+ if ( Object . keys ( filter ) . length ) {
18+ filters . push ( this . filterFieldsOrNested ( filter ) ) ;
19+ }
1920 return this . andFilterFn ( ...filters ) ;
2021 }
2122
@@ -31,16 +32,7 @@ export class FilterBuilder {
3132 return this . andFilterFn (
3233 ...Object . keys ( filter )
3334 . filter ( ( k ) => k !== 'and' && k !== 'or' )
34- . map ( ( fieldOrNested ) => {
35- const value = this . getField ( filter as FilterComparisons < DTO > , fieldOrNested as keyof DTO ) ;
36-
37- if ( isComparison ( filter [ fieldOrNested as keyof DTO ] ) ) {
38- return this . withFilterComparison ( fieldOrNested as keyof DTO , value ) ;
39- }
40-
41- const nestedFilterFn = this . build ( value ) ;
42- return ( dto ?: DTO ) => nestedFilterFn ( dto ? dto [ fieldOrNested as keyof DTO ] : null ) ;
43- } ) ,
35+ . map ( ( fieldOrNested ) => this . withComparison ( filter , fieldOrNested as keyof DTO ) ) ,
4436 ) ;
4537 }
4638
@@ -62,4 +54,16 @@ export class FilterBuilder {
6254 ) ,
6355 ) ;
6456 }
57+
58+ private static withComparison < DTO > ( filter : FilterComparisons < DTO > , fieldOrNested : keyof DTO ) : FilterFn < DTO > {
59+ const value = this . getField ( filter , fieldOrNested ) ;
60+ if ( isComparison ( value ) ) {
61+ return this . withFilterComparison ( fieldOrNested , value ) ;
62+ }
63+ if ( typeof value !== 'object' ) {
64+ throw new Error ( `unknown comparison ${ JSON . stringify ( fieldOrNested ) } ` ) ;
65+ }
66+ const nestedFilterFn = this . build ( value ) ;
67+ return ( dto ?: DTO ) => nestedFilterFn ( dto ? dto [ fieldOrNested ] : null ) ;
68+ }
6569}
0 commit comments