Formateo
This commit is contained in:
parent
8d0002bfb4
commit
c77705b8b6
@ -1,44 +1,44 @@
|
||||
import { FiltersPrimitives } from "./Filter";
|
||||
import type { FiltersPrimitives } from "./Filter";
|
||||
import { Filters } from "./Filters";
|
||||
import { InvalidCriteria } from "./InvalidCriteria";
|
||||
import { Order } from "./Order";
|
||||
|
||||
export class Criteria {
|
||||
constructor(
|
||||
public readonly filters: Filters,
|
||||
public readonly order: Order,
|
||||
public readonly pageSize: number | null,
|
||||
public readonly pageNumber: number | null,
|
||||
) {
|
||||
if (pageNumber !== null && pageSize === null) {
|
||||
throw new InvalidCriteria();
|
||||
}
|
||||
}
|
||||
constructor(
|
||||
public readonly filters: Filters,
|
||||
public readonly order: Order,
|
||||
public readonly pageSize: number | null,
|
||||
public readonly pageNumber: number | null
|
||||
) {
|
||||
if (pageNumber !== null && pageSize === null) {
|
||||
throw new InvalidCriteria();
|
||||
}
|
||||
}
|
||||
|
||||
static fromPrimitives(
|
||||
filters: FiltersPrimitives[],
|
||||
orderBy: string | null,
|
||||
orderType: string | null,
|
||||
pageSize: number | null,
|
||||
pageNumber: number | null,
|
||||
): Criteria {
|
||||
return new Criteria(
|
||||
Filters.fromPrimitives(filters),
|
||||
Order.fromPrimitives(orderBy, orderType),
|
||||
pageSize,
|
||||
pageNumber,
|
||||
);
|
||||
}
|
||||
static fromPrimitives(
|
||||
filters: FiltersPrimitives[],
|
||||
orderBy: string | null,
|
||||
orderType: string | null,
|
||||
pageSize: number | null,
|
||||
pageNumber: number | null
|
||||
): Criteria {
|
||||
return new Criteria(
|
||||
Filters.fromPrimitives(filters),
|
||||
Order.fromPrimitives(orderBy, orderType),
|
||||
pageSize,
|
||||
pageNumber
|
||||
);
|
||||
}
|
||||
|
||||
static withFilters(filters: FiltersPrimitives[]): Criteria {
|
||||
return Criteria.fromPrimitives(filters, null, null, null, null);
|
||||
}
|
||||
static withFilters(filters: FiltersPrimitives[]): Criteria {
|
||||
return Criteria.fromPrimitives(filters, null, null, null, null);
|
||||
}
|
||||
|
||||
hasOrder(): boolean {
|
||||
return !this.order.isNone();
|
||||
}
|
||||
hasOrder(): boolean {
|
||||
return !this.order.isNone();
|
||||
}
|
||||
|
||||
hasFilters(): boolean {
|
||||
return !this.filters.isEmpty();
|
||||
}
|
||||
hasFilters(): boolean {
|
||||
return !this.filters.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
26
packages/rdx-criteria/src/codelytv/Filter.d.ts
vendored
26
packages/rdx-criteria/src/codelytv/Filter.d.ts
vendored
@ -1,17 +1,17 @@
|
||||
import { FilterField } from "./FilterField";
|
||||
import { FilterOperator } from "./FilterOperator";
|
||||
import { FilterValue } from "./FilterValue";
|
||||
import type { FilterField } from "./FilterField";
|
||||
import type { FilterOperator } from "./FilterOperator";
|
||||
import type { FilterValue } from "./FilterValue";
|
||||
export type FiltersPrimitives = {
|
||||
field: string;
|
||||
operator: string;
|
||||
value: string;
|
||||
field: string;
|
||||
operator: string;
|
||||
value: string;
|
||||
};
|
||||
export declare class Filter {
|
||||
readonly field: FilterField;
|
||||
readonly operator: FilterOperator;
|
||||
readonly value: FilterValue;
|
||||
constructor(field: FilterField, operator: FilterOperator, value: FilterValue);
|
||||
static fromPrimitives(field: string, operator: string, value: string): Filter;
|
||||
toPrimitives(): FiltersPrimitives;
|
||||
readonly field: FilterField;
|
||||
readonly operator: FilterOperator;
|
||||
readonly value: FilterValue;
|
||||
constructor(field: FilterField, operator: FilterOperator, value: FilterValue);
|
||||
static fromPrimitives(field: string, operator: string, value: string): Filter;
|
||||
toPrimitives(): FiltersPrimitives;
|
||||
}
|
||||
//# sourceMappingURL=Filter.d.ts.map
|
||||
//# sourceMappingURL=Filter.d.ts.map
|
||||
|
||||
@ -3,35 +3,35 @@ import { FilterOperator, Operator } from "./FilterOperator";
|
||||
import { FilterValue } from "./FilterValue";
|
||||
|
||||
export type FiltersPrimitives = {
|
||||
field: string;
|
||||
operator: string;
|
||||
value: string;
|
||||
field: string;
|
||||
operator: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
export class Filter {
|
||||
readonly field: FilterField;
|
||||
readonly operator: FilterOperator;
|
||||
readonly value: FilterValue;
|
||||
readonly field: FilterField;
|
||||
readonly operator: FilterOperator;
|
||||
readonly value: FilterValue;
|
||||
|
||||
constructor(field: FilterField, operator: FilterOperator, value: FilterValue) {
|
||||
this.field = field;
|
||||
this.operator = operator;
|
||||
this.value = value;
|
||||
}
|
||||
constructor(field: FilterField, operator: FilterOperator, value: FilterValue) {
|
||||
this.field = field;
|
||||
this.operator = operator;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static fromPrimitives(field: string, operator: string, value: string): Filter {
|
||||
return new Filter(
|
||||
new FilterField(field),
|
||||
new FilterOperator(Operator[operator as keyof typeof Operator]),
|
||||
new FilterValue(value),
|
||||
);
|
||||
}
|
||||
static fromPrimitives(field: string, operator: string, value: string): Filter {
|
||||
return new Filter(
|
||||
new FilterField(field),
|
||||
new FilterOperator(Operator[operator as keyof typeof Operator]),
|
||||
new FilterValue(value)
|
||||
);
|
||||
}
|
||||
|
||||
toPrimitives(): FiltersPrimitives {
|
||||
return {
|
||||
field: this.field.value,
|
||||
operator: this.operator.value,
|
||||
value: this.value.value,
|
||||
};
|
||||
}
|
||||
toPrimitives(): FiltersPrimitives {
|
||||
return {
|
||||
field: this.field.value,
|
||||
operator: this.operator.value,
|
||||
value: this.value.value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export class FilterField {
|
||||
constructor(public readonly value: string) {}
|
||||
constructor(public readonly value: string) {}
|
||||
}
|
||||
|
||||
@ -1,42 +1,42 @@
|
||||
export enum Operator {
|
||||
EQUAL = "=",
|
||||
NOT_EQUAL = "!=",
|
||||
GREATER_THAN = ">",
|
||||
GREATER_THAN_OR_EQUAL = ">=",
|
||||
LOWER_THAN = "<",
|
||||
LOWER_THAN_OR_EQUAL = "<=",
|
||||
CONTAINS = "CONTAINS",
|
||||
NOT_CONTAINS = "NOT_CONTAINS",
|
||||
EQUAL = "=",
|
||||
NOT_EQUAL = "!=",
|
||||
GREATER_THAN = ">",
|
||||
GREATER_THAN_OR_EQUAL = ">=",
|
||||
LOWER_THAN = "<",
|
||||
LOWER_THAN_OR_EQUAL = "<=",
|
||||
CONTAINS = "CONTAINS",
|
||||
NOT_CONTAINS = "NOT_CONTAINS",
|
||||
}
|
||||
|
||||
export class FilterOperator {
|
||||
constructor(public readonly value: Operator) {}
|
||||
constructor(public readonly value: Operator) {}
|
||||
|
||||
isContains(): boolean {
|
||||
return this.value.valueOf() === Operator.CONTAINS.valueOf();
|
||||
}
|
||||
isContains(): boolean {
|
||||
return this.value.valueOf() === Operator.CONTAINS.valueOf();
|
||||
}
|
||||
|
||||
isNotContains(): boolean {
|
||||
return this.value.valueOf() === Operator.NOT_CONTAINS.valueOf();
|
||||
}
|
||||
isNotContains(): boolean {
|
||||
return this.value.valueOf() === Operator.NOT_CONTAINS.valueOf();
|
||||
}
|
||||
|
||||
isNotEquals(): boolean {
|
||||
return this.value.valueOf() === Operator.NOT_EQUAL.valueOf();
|
||||
}
|
||||
isNotEquals(): boolean {
|
||||
return this.value.valueOf() === Operator.NOT_EQUAL.valueOf();
|
||||
}
|
||||
|
||||
isGreaterThan(): boolean {
|
||||
return this.value.valueOf() === Operator.GREATER_THAN.valueOf();
|
||||
}
|
||||
isGreaterThan(): boolean {
|
||||
return this.value.valueOf() === Operator.GREATER_THAN.valueOf();
|
||||
}
|
||||
|
||||
isGreaterThanOrEqual(): boolean {
|
||||
return this.value.valueOf() === Operator.GREATER_THAN_OR_EQUAL.valueOf();
|
||||
}
|
||||
isGreaterThanOrEqual(): boolean {
|
||||
return this.value.valueOf() === Operator.GREATER_THAN_OR_EQUAL.valueOf();
|
||||
}
|
||||
|
||||
isLowerThan(): boolean {
|
||||
return this.value.valueOf() === Operator.LOWER_THAN.valueOf();
|
||||
}
|
||||
isLowerThan(): boolean {
|
||||
return this.value.valueOf() === Operator.LOWER_THAN.valueOf();
|
||||
}
|
||||
|
||||
isLowerThanOrEqual(): boolean {
|
||||
return this.value.valueOf() === Operator.LOWER_THAN_OR_EQUAL.valueOf();
|
||||
}
|
||||
isLowerThanOrEqual(): boolean {
|
||||
return this.value.valueOf() === Operator.LOWER_THAN_OR_EQUAL.valueOf();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export class FilterValue {
|
||||
constructor(public readonly value: string) {}
|
||||
constructor(public readonly value: string) {}
|
||||
}
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
import { Filter, FiltersPrimitives } from "./Filter";
|
||||
import { Filter, type FiltersPrimitives } from "./Filter";
|
||||
|
||||
export class Filters {
|
||||
constructor(public readonly value: Filter[]) {}
|
||||
constructor(public readonly value: Filter[]) {}
|
||||
|
||||
static fromPrimitives(filters: FiltersPrimitives[]): Filters {
|
||||
return new Filters(
|
||||
filters.map((filter) => Filter.fromPrimitives(filter.field, filter.operator, filter.value)),
|
||||
);
|
||||
}
|
||||
static fromPrimitives(filters: FiltersPrimitives[]): Filters {
|
||||
return new Filters(
|
||||
filters.map((filter) => Filter.fromPrimitives(filter.field, filter.operator, filter.value))
|
||||
);
|
||||
}
|
||||
|
||||
toPrimitives(): FiltersPrimitives[] {
|
||||
return this.value.map((filter) => filter.toPrimitives());
|
||||
}
|
||||
toPrimitives(): FiltersPrimitives[] {
|
||||
return this.value.map((filter) => filter.toPrimitives());
|
||||
}
|
||||
|
||||
isEmpty(): boolean {
|
||||
return this.value.length === 0;
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.value.length === 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
export class InvalidCriteria extends Error {
|
||||
constructor() {
|
||||
super("Page size is required when page number is defined");
|
||||
}
|
||||
constructor() {
|
||||
super("Page size is required when page number is defined");
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,22 +2,22 @@ import { OrderBy } from "./OrderBy";
|
||||
import { OrderType, OrderTypes } from "./OrderType";
|
||||
|
||||
export class Order {
|
||||
constructor(
|
||||
public readonly orderBy: OrderBy,
|
||||
public readonly orderType: OrderType,
|
||||
) {}
|
||||
constructor(
|
||||
public readonly orderBy: OrderBy,
|
||||
public readonly orderType: OrderType
|
||||
) {}
|
||||
|
||||
static none(): Order {
|
||||
return new Order(new OrderBy(""), new OrderType(OrderTypes.NONE));
|
||||
}
|
||||
static none(): Order {
|
||||
return new Order(new OrderBy(""), new OrderType(OrderTypes.NONE));
|
||||
}
|
||||
|
||||
static fromPrimitives(orderBy: string | null, orderType: string | null): Order {
|
||||
return orderBy !== null
|
||||
? new Order(new OrderBy(orderBy), new OrderType(orderType as OrderTypes))
|
||||
: Order.none();
|
||||
}
|
||||
static fromPrimitives(orderBy: string | null, orderType: string | null): Order {
|
||||
return orderBy !== null
|
||||
? new Order(new OrderBy(orderBy), new OrderType(orderType as OrderTypes))
|
||||
: Order.none();
|
||||
}
|
||||
|
||||
isNone(): boolean {
|
||||
return this.orderType.isNone();
|
||||
}
|
||||
isNone(): boolean {
|
||||
return this.orderType.isNone();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export class OrderBy {
|
||||
constructor(public readonly value: string) {}
|
||||
constructor(public readonly value: string) {}
|
||||
}
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
export enum OrderTypes {
|
||||
ASC = "ASC",
|
||||
DESC = "DESC",
|
||||
NONE = "NONE",
|
||||
ASC = "ASC",
|
||||
DESC = "DESC",
|
||||
NONE = "NONE",
|
||||
}
|
||||
|
||||
export class OrderType {
|
||||
constructor(public readonly value: OrderTypes) {}
|
||||
constructor(public readonly value: OrderTypes) {}
|
||||
|
||||
isNone(): boolean {
|
||||
return this.value === OrderTypes.NONE;
|
||||
}
|
||||
isNone(): boolean {
|
||||
return this.value === OrderTypes.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Criteria as BaseCriteria, Filters, FiltersPrimitives, Order } from "./codelytv";
|
||||
import { Criteria as BaseCriteria, Filters, type FiltersPrimitives, Order } from "./codelytv";
|
||||
import { INITIAL_PAGE_INDEX, INITIAL_PAGE_SIZE } from "./defaults";
|
||||
|
||||
export class Criteria extends BaseCriteria {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { FiltersPrimitives } from "./codelytv";
|
||||
import type { FiltersPrimitives } from "./codelytv";
|
||||
import { Criteria } from "./critera";
|
||||
import { DEFAULT_ORDER, INITIAL_PAGE_INDEX, INITIAL_PAGE_SIZE } from "./defaults";
|
||||
|
||||
@ -64,7 +64,7 @@ export class CriteriaFromUrlConverter {
|
||||
searchParams.forEach((value, key) => {
|
||||
const match = key.match(/filters\[(\d+)]\[(.+)]/);
|
||||
if (match) {
|
||||
const index = match[1];
|
||||
const index = match[1]!;
|
||||
const property = match[2] as keyof FiltersPrimitives;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!tempFilters[index]) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { FindOptions, Sequelize } from "sequelize";
|
||||
import { Criteria } from "./critera.js";
|
||||
import type { FindOptions, Sequelize } from "sequelize";
|
||||
|
||||
import type { Criteria } from "./critera.js";
|
||||
|
||||
/**
|
||||
* Mapeo lógico→físico de campos.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user