@@ -83,9 +83,11 @@ export type QueryOptions<T> = ExpandOptions<T> & {
83
83
action : string ;
84
84
func : string | { [ functionName : string ] : { [ parameterName : string ] : any } } ;
85
85
format : string ;
86
- aliases : Alias [ ] ;
86
+ aliases : Alias [ ] ;
87
87
}
88
88
89
+ export const ITEM_ROOT = "" ;
90
+
89
91
export default function < T > ( {
90
92
select : $select ,
91
93
search : $search ,
@@ -183,10 +185,12 @@ function buildFilter(filters: Filter = {}, propPrefix = ''): string {
183
185
const value = ( filter as any ) [ filterKey ] ;
184
186
let propName = '' ;
185
187
if ( propPrefix ) {
186
- if ( INDEXOF_REGEX . test ( filterKey ) ) {
187
- propName = filterKey . replace ( INDEXOF_REGEX , `(${ propPrefix } /$1)` ) ;
188
+ if ( filterKey === ITEM_ROOT ) {
189
+ propName = propPrefix ;
190
+ } else if ( INDEXOF_REGEX . test ( filterKey ) ) {
191
+ propName = filterKey . replace ( INDEXOF_REGEX , ( _ , $1 ) => $1 . trim ( ) === ITEM_ROOT ? `(${ propPrefix } )` : `(${ propPrefix } /${ $1 . trim ( ) } )` ) ;
188
192
} else if ( FUNCTION_REGEX . test ( filterKey ) ) {
189
- propName = filterKey . replace ( FUNCTION_REGEX , `(${ propPrefix } /$1 )` ) ;
193
+ propName = filterKey . replace ( FUNCTION_REGEX , ( _ , $1 ) => $1 . trim ( ) === ITEM_ROOT ? `(${ propPrefix } )` : `( ${ propPrefix } / ${ $1 . trim ( ) } )`) ;
190
194
} else {
191
195
propName = `${ propPrefix } /${ filterKey } ` ;
192
196
}
@@ -490,7 +494,7 @@ function buildGroupBy<T>(groupBy: GroupBy<T>) {
490
494
function buildOrderBy < T > ( orderBy : OrderBy < T > , prefix : string = '' ) : string {
491
495
if ( Array . isArray ( orderBy ) ) {
492
496
return ( orderBy as OrderByOptions < T > [ ] )
493
- . map ( value =>
497
+ . map ( value =>
494
498
( Array . isArray ( value ) && value . length === 2 && [ 'asc' , 'desc' ] . indexOf ( value [ 1 ] ) !== - 1 ) ? value . join ( ' ' ) : value
495
499
)
496
500
. map ( v => `${ prefix } ${ v } ` ) . join ( ',' ) ;
@@ -506,7 +510,7 @@ function buildUrl(path: string, params: PlainObject): string {
506
510
// This can be refactored using URL API. But IE does not support it.
507
511
const queries : string [ ] = [ ] ;
508
512
for ( const key of Object . getOwnPropertyNames ( params ) ) {
509
- const value = params [ key ] ;
513
+ const value = params [ key ] ;
510
514
if ( value === 0 || ! ! value ) {
511
515
queries . push ( `${ key } =${ value } ` ) ;
512
516
}
@@ -516,4 +520,4 @@ function buildUrl(path: string, params: PlainObject): string {
516
520
517
521
function parseNot ( builtFilters : string [ ] ) : string {
518
522
return `not(${ builtFilters . join ( ' and ' ) } )` ;
519
- }
523
+ }
0 commit comments