|
|
|
@ -28,30 +28,89 @@ |
|
|
|
|
|
|
|
|
|
export type FilterOperator = '='|'!*'|'!'|'~'|'o'|'>t-'|'<>d'|'**'|'ow' ; |
|
|
|
|
|
|
|
|
|
export interface ApiV3FilterValue { |
|
|
|
|
operator:FilterOperator; |
|
|
|
|
values:any; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface ApiV3Filter { |
|
|
|
|
[filter:string]:{ operator:FilterOperator, values:any }; |
|
|
|
|
[filter:string]:ApiV3FilterValue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export type ApiV3FilterObject = { [filter:string]:ApiV3FilterValue }; |
|
|
|
|
|
|
|
|
|
export class ApiV3FilterBuilder { |
|
|
|
|
|
|
|
|
|
public filters:ApiV3Filter[] = []; |
|
|
|
|
private filterMap:ApiV3FilterObject = {}; |
|
|
|
|
|
|
|
|
|
public add(name:string, operator:FilterOperator, values:any):this { |
|
|
|
|
let newFilter:ApiV3Filter = {}; |
|
|
|
|
newFilter[name] = { |
|
|
|
|
this.filterMap[name] = { |
|
|
|
|
operator: operator, |
|
|
|
|
values: values |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
this.filters.push(newFilter); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Remove from the filter set |
|
|
|
|
* @param name |
|
|
|
|
*/ |
|
|
|
|
public remove(name:string) { |
|
|
|
|
delete this.filterMap[name]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Turns the array-map style of query filters to an actual object |
|
|
|
|
* |
|
|
|
|
* @param filters APIv3 filter array [ {foo: { operator: '=', val: ['bar'] } }, ...] |
|
|
|
|
* @return A map { foo: { operator: '=', val: ['bar'] } , ... } |
|
|
|
|
*/ |
|
|
|
|
public toFilterObject(filters:ApiV3Filter[]):ApiV3FilterObject { |
|
|
|
|
let map:ApiV3FilterObject = {}; |
|
|
|
|
|
|
|
|
|
filters.forEach((item:ApiV3Filter) => { |
|
|
|
|
_.each(item, (val:ApiV3FilterValue, filter:string) => { |
|
|
|
|
map[filter] = val; |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return map; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Merges the other filters into the current set, |
|
|
|
|
* replacing them if the are duplicated. |
|
|
|
|
* |
|
|
|
|
* @param filters |
|
|
|
|
* @param only Only apply the given filters |
|
|
|
|
*/ |
|
|
|
|
public merge(filters:ApiV3Filter[], ...only:string[]) { |
|
|
|
|
const toAdd:ApiV3FilterObject = _.pickBy( |
|
|
|
|
this.toFilterObject(filters), |
|
|
|
|
(_, filter:string) => only.includes(filter) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
this.filterMap = { |
|
|
|
|
...this.filterMap, |
|
|
|
|
...toAdd |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public get filters():ApiV3Filter[] { |
|
|
|
|
let filters:ApiV3Filter[] = []; |
|
|
|
|
_.each(this.filterMap, (val:ApiV3FilterValue, filter:string) => { |
|
|
|
|
filters.push({ [filter]: val }); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return filters; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public toJson():string { |
|
|
|
|
return JSON.stringify(this.filters); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public toParams(mergeParams:{[key:string]:string} = {}):string { |
|
|
|
|
public toParams(mergeParams:{ [key:string]:string } = {}):string { |
|
|
|
|
let transformedFilters:string[] = []; |
|
|
|
|
|
|
|
|
|
transformedFilters = this.filters.map((filter:ApiV3Filter) => { |
|
|
|
|