Advertisement
Guest User

Untitled

a guest
May 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import _ from 'lodash';
  2.  
  3. const GENERAL_FILTER = '_GENERAL_FILTER_FIELD_';
  4.  
  5. const FilterTypes = {
  6.   EQUALS: 'equals',
  7.   NOT_EQUALS: 'notEquals',
  8.   NULL: 'null',
  9.   NOT_NULL: 'notNull',
  10.   LIKE: 'like',
  11.   NOT_LIKE: 'notLike',
  12.   GT: 'gt',
  13.   GTE: 'gte',
  14.   LT: 'lt',
  15.   LTE: 'lte',
  16.   IN: 'in'
  17. };
  18.  
  19. function joinFilters(builder, filtersParam, joinType) {
  20.   let filters;
  21.   if (!_.isArray(filtersParam)) {
  22.     filters = [filtersParam];
  23.   }
  24.  
  25.   filters.forEach(filter => {
  26.     builder.booleanJoins.push({
  27.       joinType,
  28.       filter
  29.     });
  30.   });
  31.   return filters;
  32. }
  33.  
  34. function addCondition(builder, condition, other) {
  35.   const cond = { condition };
  36.  
  37.   if (other || other === false) {
  38.     if (_.isArray(other)) {
  39.       cond.comparingObjects = [];
  40.       other.forEach(value => {
  41.         const conditionValue = { value };
  42.         if (_.isString(value)) {
  43.           conditionValue.type = 'string';
  44.         } else if (_.isNumber(value)) {
  45.           conditionValue.type = 'integer';
  46.         }
  47.         cond.comparingObjects.push(conditionValue);
  48.       });
  49.     } else {
  50.       cond.comparingObject = other;
  51.  
  52.       if (_.isBoolean(other)) {
  53.         cond.type = 'boolean';
  54.       } else if (_.isString(other)) {
  55.         cond.type = 'string';
  56.       } else if (_.isDate(other)) {
  57.         cond.type = 'date';
  58.       } else if (_.isNumber(other)) {
  59.         cond.type = 'integer';
  60.       }
  61.     }
  62.   }
  63.   builder.conditions.push(cond);
  64. }
  65.  
  66. class Filter {
  67.   constructor(path) {
  68.     this.path = path;
  69.     this.conditions = [];
  70.     this.booleanJoins = [];
  71.   }
  72.  
  73.   or(filters) {
  74.     joinFilters(this, filters, 'or');
  75.     return this;
  76.   }
  77.  
  78.   and(filters) {
  79.     joinFilters(this, filters, 'and');
  80.     return this;
  81.   }
  82.  
  83.   eq(other) {
  84.     addCondition(this, FilterTypes.EQUALS, other);
  85.     return this;
  86.   }
  87.  
  88.   notEq(other) {
  89.     addCondition(this, FilterTypes.NOT_EQUALS, other);
  90.     return this;
  91.   }
  92.  
  93.   isNull() {
  94.     addCondition(this, FilterTypes.NULL);
  95.     return this;
  96.   }
  97.  
  98.   notNull() {
  99.     addCondition(this, FilterTypes.NOT_NULL);
  100.     return this;
  101.   }
  102.  
  103.   like(other) {
  104.     addCondition(this, FilterTypes.LIKE, other);
  105.     return this;
  106.   }
  107.  
  108.   notLike(other) {
  109.     addCondition(this, FilterTypes.NOT_LIKE, other);
  110.     return this;
  111.   }
  112.  
  113.   gt(other) {
  114.     addCondition(this, FilterTypes.GT, other);
  115.     return this;
  116.   }
  117.  
  118.   gte(other) {
  119.     addCondition(this, FilterTypes.GTE, other);
  120.     return this;
  121.   }
  122.  
  123.   lt(other) {
  124.     addCondition(this, FilterTypes.LT, other);
  125.     return this;
  126.   }
  127.  
  128.   lte(other) {
  129.     addCondition(this, FilterTypes.LTE, other);
  130.     return this;
  131.   }
  132.  
  133.   in(other) {
  134.     addCondition(this, FilterTypes.IN, other);
  135.     return this;
  136.   }
  137.  
  138.   fromCondition(condition, value) {
  139.     switch (condition) {
  140.       case FilterTypes.EQUALS:
  141.         this.eq(value);
  142.         break;
  143.       case FilterTypes.NOT_EQUALS:
  144.         this.notEq(value);
  145.         break;
  146.       case FilterTypes.LIKE:
  147.         this.like(value);
  148.         break;
  149.       case FilterTypes.NOT_LIKE:
  150.         this.notLike(value);
  151.         break;
  152.       case FilterTypes.NULL:
  153.         this.isNull();
  154.         break;
  155.       case FilterTypes.NOT_NULL:
  156.         this.notNull();
  157.         break;
  158.       case FilterTypes.GT:
  159.         this.gt(value);
  160.         break;
  161.       case FilterTypes.GTE:
  162.         this.gte(value);
  163.         break;
  164.       case FilterTypes.LT:
  165.         this.lt(value);
  166.         break;
  167.       case FilterTypes.LTE:
  168.         this.lte(value);
  169.         break;
  170.       default:
  171.         console.log('Condition Type not found');
  172.     }
  173.     return this;
  174.   }
  175.  
  176.   static path(filterPath) {
  177.     return new Filter(filterPath);
  178.   }
  179. }
  180.  
  181. const createGeneralFilter = (attributes, value) => {
  182.   let filter;
  183.   attributes.forEach(attribute => {
  184.     if (!filter) {
  185.       filter = Filter.path(attribute).like(value);
  186.     } else {
  187.       filter.or(Filter.path(attribute).like(value));
  188.     }
  189.   });
  190.   return filter;
  191. };
  192.  
  193. const createFilter = (rawFilters, attributes) => {
  194.   console.log(`FIltros ${JSON.stringify(rawFilters)}`)
  195.   let filters;
  196.   rawFilters.forEach(filter => {
  197.     let value = filter.value;
  198.     if (filter.type === 'boolean') {
  199.       value = filter.value === 'true' ? true : false;
  200.     }
  201.     if (filter.key === GENERAL_FILTER) {
  202.       if (!filters) {
  203.         filters = createGeneralFilter(attributes, value);
  204.       } else {
  205.         filters = filters.and(createGeneralFilter(attributes, value));
  206.       }
  207.     } else if (!filters) {
  208.       filters = Filter.path(filter.key).fromCondition(
  209.         filter.condition,
  210.         value
  211.       );
  212.     } else {
  213.       filters = filters.and(
  214.         Filter.path(filter.key).fromCondition(filter.condition, value)
  215.       );
  216.     }
  217.   });
  218.   return filters;
  219. };
  220.  
  221. export default createFilter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement