All files / json-expression/src/operators logical.ts

100% Statements 15/15
100% Branches 4/4
100% Functions 10/10
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 443x     3x           272x     46x 17x                 198x     26x 12x                 58x     13x 13x        
import {Expression, type ExpressionResult} from '../codegen-steps';
import type * as types from '../types';
 
export const logicalOperators: types.OperatorDefinition<any>[] = [
  [
    '&&',
    ['and'],
    -1,
    (expr: types.ExprAnd, ctx) => {
      return expr.slice(1).reduce((acc, e) => acc && ctx.eval(e, ctx), true);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprAnd>): ExpressionResult => {
      const js = ctx.operands.map((expr) => `(${expr})`).join('&&');
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprAnd>,
 
  [
    '||',
    ['or'],
    -1,
    (expr: types.ExprOr, ctx) => {
      return expr.slice(1).reduce((acc, e) => acc || ctx.eval(e, ctx), false);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprOr>): ExpressionResult => {
      const js = ctx.operands.map((expr) => `(${expr})`).join('||');
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprOr>,
 
  [
    '!',
    ['not'],
    1,
    (expr: types.ExprNot, ctx) => {
      return !ctx.eval(expr[1], ctx);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprNot>): ExpressionResult => {
      const js = `!(${ctx.operands[0]})`;
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprNot>,
];