import query from './query.pegjs';

export function parseToAst (string) {
  return query.parse(string);
}

const opfunc = {
  '<=': function (x, y) {
    return x <= y;
  },

  '<': function (x, y) {
    return x < y;
  },

  '>=': function (x, y) {
    return x >= y;
  },

  '>': function (x, y) {
    return x > y;
  },

  '=': function (x, y) {
    return x == y;
  },

  '!=': function (x, y) {
    return x != y;
  }
};

function checker (ast, include) {
  return function (row) {
    const data = row[ast.operands[0]];
    for (let i = 0; i < ast.operands[1].length; i++) {
      if (data === ast.operands[1][i]) {
        return include;
      }
    }

    return !include;
  }
}

export function astToFunction (ast) {
  switch (ast.operator) {
  case 'or':
    return function (row) {
      const f1 = parseToFunction(ast.operands[0]);
      const f2 = parseToFunction(ast.operands[1]);

      return f1(row) || f2(row);
    };
    break;

  case 'and':
    return function (row) {
      const f1 = parseToFunction(ast.operands[0]);
      const f2 = parseToFunction(ast.operands[1]);

      return f1(row) && f2(row);
    };
    break;

  case 'not':
    return function (row) {
      const f = astToFunction(ast.operands);
      return !f(row);
    };
    break;

  case 'in':
    return checker(ast, true);
    break;

  case 'not in':
    return checker(ast, false);
    break;

  case '<=':
  case '<':
  case '>=':
  case '>':
  case '=':
  case '!=':
    return function (row) {
      const data = row[ast.operands[0]];
      return opfunc[ast.operator](data, ast.operands[1])
    }
    break;
  }
}

export function parseToFunction (string) {
  return astToFunction(parseToAst(string));
}

@@ -357,16 +357,17 @@
 urn x ==
+=
  y;%0A  %7D,
@@ -409,16 +409,17 @@
 urn x !=
+=
  y;%0A  %7D%0A
@@ -676,24 +676,25 @@
 include;%0A  %7D
+;
 %0A%7D%0A%0Aexport f
@@ -749,16 +749,18 @@
 ator) %7B%0A
+  
   case '
@@ -764,16 +764,18 @@
 e 'or':%0A
+  
     retu
@@ -786,32 +786,34 @@
 unction (row) %7B%0A
+  
       const f1 =
@@ -839,32 +839,34 @@
 t.operands%5B0%5D);%0A
+  
       const f2 =
@@ -899,32 +899,34 @@
 nds%5B1%5D);%0A%0A      
+  
 return f1(row) %7C
@@ -936,39 +936,32 @@
 2(row);%0A    
+  
 %7D;%0A
+%0A
   
-  break;%0A%0A
   case 'and'
@@ -962,16 +962,18 @@
  'and':%0A
+  
     retu
@@ -984,32 +984,34 @@
 unction (row) %7B%0A
+  
       const f1 =
@@ -1041,24 +1041,26 @@
 erands%5B0%5D);%0A
+  
       const 
@@ -1101,24 +1101,26 @@
 1%5D);%0A%0A      
+  
 return f1(ro
@@ -1134,39 +1134,32 @@
 2(row);%0A    
+  
 %7D;%0A
- 
+%0A
   
- break;%0A%0A
   case 'not'
@@ -1160,24 +1160,26 @@
  'not':%0A    
+  
 return funct
@@ -1182,32 +1182,34 @@
 unction (row) %7B%0A
+  
       const f = 
@@ -1239,24 +1239,26 @@
 nds);%0A      
+  
 return !f(ro
@@ -1269,23 +1269,16 @@
     
-%7D;%0A    break
+  %7D
 ;%0A%0A
+  
   ca
@@ -1282,24 +1282,26 @@
  case 'in':%0A
+  
     return c
@@ -1315,36 +1315,27 @@
 ast, true);%0A
+%0A
   
-  break;%0A%0A
   case 'not 
@@ -1335,24 +1335,26 @@
 e 'not in':%0A
+  
     return c
@@ -1377,20 +1377,11 @@
 e);%0A
+%0A
   
-  break;%0A%0A
   ca
@@ -1385,24 +1385,26 @@
  case '%3C=':%0A
+  
   case '%3C':%0A
@@ -1403,16 +1403,18 @@
 se '%3C':%0A
+  
   case '
@@ -1414,24 +1414,26 @@
  case '%3E=':%0A
+  
   case '%3E':%0A
@@ -1432,16 +1432,18 @@
 se '%3E':%0A
+  
   case '
@@ -1446,16 +1446,18 @@
 se '=':%0A
+  
   case '
@@ -1461,24 +1461,26 @@
 e '!=':%0A    
+  
 return funct
@@ -1493,24 +1493,26 @@
 ow) %7B%0A      
+ 
+ 
 const data =
@@ -1530,24 +1530,26 @@
 erands%5B0%5D%5D;%0A
+  
       return
@@ -1596,24 +1596,17 @@
 %5B1%5D)
+;
 %0A    
-%7D%0A    break
+  %7D
 ;%0A  

Bring query language module up to lint standards