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

97.14% Statements 68/70
100% Branches 14/14
100% Functions 25/25
96.96% Lines 64/66

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 1963x 3x 3x 3x     3x           18x 18x 18x     3x       330x 330x 330x     3x       91x 91x 91x 91x     3x           108x 108x     17x 17x 17x 13x 2x 2x                 59x 59x     3x 3x 3x 1x                     33x 33x     11x                 41x 41x     10x                 38x 38x     12x                 40x 40x     8x                 11x 11x     1x 1x                 29x 29x     11x 11x                 16x 16x     6x 6x                 22x 22x     8x 8x                 24x 24x     10x 10x        
import {Expression, type ExpressionResult, Literal} from '../codegen-steps';
import {deepEqual} from '@jsonjoy.com/util/lib/json-equal/deepEqual';
import {deepEqualCodegen} from '@jsonjoy.com/util/lib/json-equal/deepEqualCodegen';
import * as util from '../util';
import type * as types from '../types';
 
const eqLitVsExpr = (
  literal: Literal,
  expression: Expression,
  ctx: types.OperatorCodegenCtx<types.Expression>,
  not?: boolean,
): ExpressionResult => {
  const fn = deepEqualCodegen(literal.val);
  const d = ctx.const(fn);
  return new Expression(`${not ? '!' : ''}${d}(${expression})`);
};
 
const binaryOperands = (
  expr: types.BinaryExpression<any>,
  ctx: types.OperatorEvalCtx,
): [left: unknown, right: unknown] => {
  const left = ctx.eval(expr[1], ctx);
  const right = ctx.eval(expr[2], ctx);
  return [left, right];
};
 
const ternaryOperands = (
  expr: types.TernaryExpression<any>,
  ctx: types.OperatorEvalCtx,
): [a: unknown, b: unknown, c: unknown] => {
  const a = ctx.eval(expr[1], ctx);
  const b = ctx.eval(expr[2], ctx);
  const c = ctx.eval(expr[3], ctx);
  return [a, b, c];
};
 
export const comparisonOperators: types.OperatorDefinition<any>[] = [
  [
    '==',
    ['eq'],
    2,
    (expr: types.ExprEquals, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return deepEqual(left, right);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprEquals>): ExpressionResult => {
      const a = ctx.operands[0];
      const b = ctx.operands[1];
      if (a instanceof Literal && b instanceof Expression) return eqLitVsExpr(a, b, ctx);
      if (b instanceof Literal && a instanceof Expression) return eqLitVsExpr(b, a, ctx);
      ctx.link(deepEqual, 'deepEqual');
      return new Expression(`deepEqual(${a},${b})`);
    },
  ] as types.OperatorDefinition<types.ExprEquals>,
 
  [
    '!=',
    ['ne'],
    2,
    (expr: types.ExprNotEquals, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return !deepEqual(left, right);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprNotEquals>): ExpressionResult => {
      const a = ctx.operands[0];
      const b = ctx.operands[1];
      if (a instanceof Literal && b instanceof Expression) return eqLitVsExpr(a, b, ctx, true);
      if (b instanceof Literal && a instanceof Expression) return eqLitVsExpr(b, a, ctx, true);
      ctx.link(deepEqual, 'deepEqual');
      return new Expression(`!deepEqual(${a},${b})`);
    },
  ] as types.OperatorDefinition<types.ExprNotEquals>,
 
  [
    '>',
    ['gt'],
    2,
    (expr: types.ExprGreaterThan, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return <any>left > <any>right;
    },
    (ctx: types.OperatorCodegenCtx<types.ExprGreaterThan>): ExpressionResult => {
      return new Expression(`(${ctx.operands[0]})>(${ctx.operands[1]})`);
    },
  ] as types.OperatorDefinition<types.ExprGreaterThan>,
 
  [
    '>=',
    ['ge'],
    2,
    (expr: types.ExprGreaterThanOrEqual, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return <any>left >= <any>right;
    },
    (ctx: types.OperatorCodegenCtx<types.ExprGreaterThanOrEqual>): ExpressionResult => {
      return new Expression(`(${ctx.operands[0]})>=(${ctx.operands[1]})`);
    },
  ] as types.OperatorDefinition<types.ExprGreaterThanOrEqual>,
 
  [
    '<',
    ['lt'],
    2,
    (expr: types.ExprLessThan, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return <any>left < <any>right;
    },
    (ctx: types.OperatorCodegenCtx<types.ExprLessThan>): ExpressionResult => {
      return new Expression(`(${ctx.operands[0]})<(${ctx.operands[1]})`);
    },
  ] as types.OperatorDefinition<types.ExprLessThan>,
 
  [
    '<=',
    ['le'],
    2,
    (expr: types.ExprLessThanOrEqual, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return <any>left <= <any>right;
    },
    (ctx: types.OperatorCodegenCtx<types.ExprLessThanOrEqual>): ExpressionResult => {
      return new Expression(`(${ctx.operands[0]})<=(${ctx.operands[1]})`);
    },
  ] as types.OperatorDefinition<types.ExprLessThanOrEqual>,
 
  [
    'cmp',
    [],
    2,
    (expr: types.ExprCmp, ctx) => {
      const [left, right] = binaryOperands(expr, ctx);
      return util.cmp(left, right);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprCmp>): ExpressionResult => {
      ctx.link(util.cmp, 'cmp');
      return new Expression(`cmp((${ctx.operands[0]}),(${ctx.operands[1]}))`);
    },
  ] as types.OperatorDefinition<types.ExprCmp>,
 
  [
    '=><=',
    ['between'],
    3,
    (expr: types.ExprBetweenEqEq, ctx) => {
      const [val, min, max] = ternaryOperands(expr, ctx);
      return util.betweenEqEq(val, min, max);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprBetweenEqEq>): ExpressionResult => {
      ctx.link(util.betweenEqEq, 'betweenEqEq');
      return new Expression(`betweenEqEq(${ctx.operands[0]},${ctx.operands[1]},${ctx.operands[2]})`);
    },
  ] as types.OperatorDefinition<types.ExprBetweenEqEq>,
 
  [
    '><',
    [],
    3,
    (expr: types.ExprBetweenNeNe, ctx) => {
      const [val, min, max] = ternaryOperands(expr, ctx);
      return util.betweenNeNe(val, min, max);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprBetweenNeNe>): ExpressionResult => {
      ctx.link(util.betweenNeNe, 'betweenNeNe');
      return new Expression(`betweenNeNe(${ctx.operands[0]},${ctx.operands[1]},${ctx.operands[2]})`);
    },
  ] as types.OperatorDefinition<types.ExprBetweenNeNe>,
 
  [
    '=><',
    [],
    3,
    (expr: types.ExprBetweenEqNe, ctx) => {
      const [val, min, max] = ternaryOperands(expr, ctx);
      return util.betweenEqNe(val, min, max);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprBetweenEqNe>): ExpressionResult => {
      ctx.link(util.betweenEqNe, 'betweenEqNe');
      return new Expression(`betweenEqNe(${ctx.operands[0]},${ctx.operands[1]},${ctx.operands[2]})`);
    },
  ] as types.OperatorDefinition<types.ExprBetweenEqNe>,
 
  [
    '><=',
    [],
    3,
    (expr: types.ExprBetweenNeEq, ctx) => {
      const [val, min, max] = ternaryOperands(expr, ctx);
      return util.betweenNeEq(val, min, max);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprBetweenNeEq>): ExpressionResult => {
      ctx.link(util.betweenNeEq, 'betweenNeEq');
      return new Expression(`betweenNeEq(${ctx.operands[0]},${ctx.operands[1]},${ctx.operands[2]})`);
    },
  ] as types.OperatorDefinition<types.ExprBetweenNeEq>,
];