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