Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import _ from 'lodash';
- const GENERAL_FILTER = '_GENERAL_FILTER_FIELD_';
- const FilterTypes = {
- EQUALS: 'equals',
- NOT_EQUALS: 'notEquals',
- NULL: 'null',
- NOT_NULL: 'notNull',
- LIKE: 'like',
- NOT_LIKE: 'notLike',
- GT: 'gt',
- GTE: 'gte',
- LT: 'lt',
- LTE: 'lte',
- IN: 'in'
- };
- function joinFilters(builder, filtersParam, joinType) {
- let filters;
- if (!_.isArray(filtersParam)) {
- filters = [filtersParam];
- }
- filters.forEach(filter => {
- builder.booleanJoins.push({
- joinType,
- filter
- });
- });
- return filters;
- }
- function addCondition(builder, condition, other) {
- const cond = { condition };
- if (other || other === false) {
- if (_.isArray(other)) {
- cond.comparingObjects = [];
- other.forEach(value => {
- const conditionValue = { value };
- if (_.isString(value)) {
- conditionValue.type = 'string';
- } else if (_.isNumber(value)) {
- conditionValue.type = 'integer';
- }
- cond.comparingObjects.push(conditionValue);
- });
- } else {
- cond.comparingObject = other;
- if (_.isBoolean(other)) {
- cond.type = 'boolean';
- } else if (_.isString(other)) {
- cond.type = 'string';
- } else if (_.isDate(other)) {
- cond.type = 'date';
- } else if (_.isNumber(other)) {
- cond.type = 'integer';
- }
- }
- }
- builder.conditions.push(cond);
- }
- class Filter {
- constructor(path) {
- this.path = path;
- this.conditions = [];
- this.booleanJoins = [];
- }
- or(filters) {
- joinFilters(this, filters, 'or');
- return this;
- }
- and(filters) {
- joinFilters(this, filters, 'and');
- return this;
- }
- eq(other) {
- addCondition(this, FilterTypes.EQUALS, other);
- return this;
- }
- notEq(other) {
- addCondition(this, FilterTypes.NOT_EQUALS, other);
- return this;
- }
- isNull() {
- addCondition(this, FilterTypes.NULL);
- return this;
- }
- notNull() {
- addCondition(this, FilterTypes.NOT_NULL);
- return this;
- }
- like(other) {
- addCondition(this, FilterTypes.LIKE, other);
- return this;
- }
- notLike(other) {
- addCondition(this, FilterTypes.NOT_LIKE, other);
- return this;
- }
- gt(other) {
- addCondition(this, FilterTypes.GT, other);
- return this;
- }
- gte(other) {
- addCondition(this, FilterTypes.GTE, other);
- return this;
- }
- lt(other) {
- addCondition(this, FilterTypes.LT, other);
- return this;
- }
- lte(other) {
- addCondition(this, FilterTypes.LTE, other);
- return this;
- }
- in(other) {
- addCondition(this, FilterTypes.IN, other);
- return this;
- }
- fromCondition(condition, value) {
- switch (condition) {
- case FilterTypes.EQUALS:
- this.eq(value);
- break;
- case FilterTypes.NOT_EQUALS:
- this.notEq(value);
- break;
- case FilterTypes.LIKE:
- this.like(value);
- break;
- case FilterTypes.NOT_LIKE:
- this.notLike(value);
- break;
- case FilterTypes.NULL:
- this.isNull();
- break;
- case FilterTypes.NOT_NULL:
- this.notNull();
- break;
- case FilterTypes.GT:
- this.gt(value);
- break;
- case FilterTypes.GTE:
- this.gte(value);
- break;
- case FilterTypes.LT:
- this.lt(value);
- break;
- case FilterTypes.LTE:
- this.lte(value);
- break;
- default:
- console.log('Condition Type not found');
- }
- return this;
- }
- static path(filterPath) {
- return new Filter(filterPath);
- }
- }
- const createGeneralFilter = (attributes, value) => {
- let filter;
- attributes.forEach(attribute => {
- if (!filter) {
- filter = Filter.path(attribute).like(value);
- } else {
- filter.or(Filter.path(attribute).like(value));
- }
- });
- return filter;
- };
- const createFilter = (rawFilters, attributes) => {
- console.log(`FIltros ${JSON.stringify(rawFilters)}`)
- let filters;
- rawFilters.forEach(filter => {
- let value = filter.value;
- if (filter.type === 'boolean') {
- value = filter.value === 'true' ? true : false;
- }
- if (filter.key === GENERAL_FILTER) {
- if (!filters) {
- filters = createGeneralFilter(attributes, value);
- } else {
- filters = filters.and(createGeneralFilter(attributes, value));
- }
- } else if (!filters) {
- filters = Filter.path(filter.key).fromCondition(
- filter.condition,
- value
- );
- } else {
- filters = filters.and(
- Filter.path(filter.key).fromCondition(filter.condition, value)
- );
- }
- });
- return filters;
- };
- export default createFilter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement