find filter helpers
This commit is contained in:
parent
17f05dd9e2
commit
acda0ff525
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "axp-ts",
|
||||
"version": "1.11.1",
|
||||
"version": "1.11.2",
|
||||
"description": "TypeScript helper library",
|
||||
"author": "AntoXa PRO <info@antoxa.pro>",
|
||||
"homepage": "https://antoxahub.ru/antoxa/axp-ts",
|
||||
|
@ -35,6 +35,17 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
pagination?: TPaginationQuery
|
||||
sort?: string
|
||||
|
||||
static getQuery<T extends TQuery>(filter: TFindFilter<T>): T {
|
||||
return Object.assign(
|
||||
{ ...filter.obj, ...filter.pagination },
|
||||
{ sort: filter.sort }
|
||||
) as T
|
||||
}
|
||||
|
||||
static parse<T extends TQuery>(filter: TFindFilter<T>): FindFilter<T> {
|
||||
return new FindFilter<T>(this.getQuery(filter))
|
||||
}
|
||||
|
||||
constructor(query?: T) {
|
||||
// Copy fiends.
|
||||
let queryCopy: T = Object.assign({}, query)
|
||||
@ -59,6 +70,9 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
this.obj = queryCopy
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated.
|
||||
*/
|
||||
setPagination(pagination?: TPaginationQuery) {
|
||||
this.pagination = new Pagination(pagination).getQuery()
|
||||
}
|
||||
@ -71,20 +85,15 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
isEqual(filters: TFindFilter<T>[]) {
|
||||
return isEqual([this, ...filters])
|
||||
}
|
||||
|
||||
toString(filter?: TFindFilter<TQuery>, opt?: { base?: string }): string {
|
||||
toString(opt?: { base?: string }): string {
|
||||
let url = opt?.base?.replace(/[?]$/, '') || ''
|
||||
|
||||
if (filter) {
|
||||
const query: string[] = []
|
||||
|
||||
// Params and Pagination.
|
||||
for (const [key, val] of Object.entries({
|
||||
...filter.obj,
|
||||
...filter.pagination
|
||||
...this.obj,
|
||||
...this.pagination
|
||||
})) {
|
||||
if (val) {
|
||||
const keyEncode = encodeURIComponent(key)
|
||||
@ -94,17 +103,20 @@ export class FindFilter<T extends TQuery> implements TFindFilter<T> {
|
||||
}
|
||||
|
||||
// Sort.
|
||||
if (filter.sort) {
|
||||
if (this.sort) {
|
||||
try {
|
||||
query.push('sort=' + encodeURIComponent(filter.sort))
|
||||
query.push('sort=' + encodeURIComponent(this.sort))
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
if (query.length) {
|
||||
url += '?' + query.join('&')
|
||||
}
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
isEqual(filters: TFindFilter<T>[]) {
|
||||
return isEqual([this, ...filters])
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user