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

89.61% Statements 69/77
100% Branches 0/0
84% Functions 42/50
86.44% Lines 51/59

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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 2433x 3x     3x   3x           292x     20x 9x                 47x     4x 1x                 94x     12x 5x                 34x 42x     4x 8x 4x 4x 4x                 45x 53x     5x 10x 5x 5x 5x                 61x     3x 1x                 55x     3x 1x                 23x     4x                 27x     6x                 27x     6x                 11x                       13x                       11x                       11x                       11x                       11x 11x 11x                       11x                       11x 11x 11x              
import * as util from '../util';
import {Expression, type ExpressionResult} from '../codegen-steps';
import type * as types from '../types';
 
const toNum = util.num;
 
export const arithmeticOperators: types.OperatorDefinition<any>[] = [
  [
    '+',
    ['add'],
    -1,
    (expr: types.ExprPlus, ctx) => {
      return expr.slice(1).reduce((acc, e) => toNum(ctx.eval(e, ctx)) + acc, 0);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprPlus>): ExpressionResult => {
      const js = ctx.operands.map((expr) => `(+(${expr})||0)`).join('+');
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprPlus>,
 
  [
    '-',
    ['subtract'],
    -1,
    (expr: types.ExprMinus, ctx) => {
      return expr.slice(2).reduce((acc, e) => acc - toNum(ctx.eval(e, ctx)), toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprMinus>): ExpressionResult => {
      const js = ctx.operands.map((expr) => `(+(${expr})||0)`).join('-');
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprMinus>,
 
  [
    '*',
    ['multiply'],
    -1,
    (expr: types.ExprAsterisk, ctx) => {
      return expr.slice(1).reduce((acc, e) => toNum(ctx.eval(e, ctx)) * acc, 1);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprAsterisk>): ExpressionResult => {
      const js = ctx.operands.map((expr) => `(+(${expr})||0)`).join('*');
      return new Expression(js);
    },
  ] as types.OperatorDefinition<types.ExprAsterisk>,
 
  [
    '/',
    ['divide'],
    -1,
    (expr: types.ExprMinus, ctx) => {
      const start = toNum(ctx.eval(expr[1], ctx));
      return expr.slice(2).reduce((acc, e) => util.slash(acc, toNum(ctx.eval(e, ctx))), start);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprMinus>): ExpressionResult => {
      ctx.link(util.slash, 'slash');
      const params = ctx.operands.map((expr) => `(+(${expr})||0)`);
      let last: string = params[0];
      for (let i = 1; i < params.length; i++) last = `slash(${last}, ${params[i]})`;
      return new Expression(last);
    },
  ] as types.OperatorDefinition<types.ExprMinus>,
 
  [
    '%',
    ['mod'],
    -1,
    (expr: types.ExprMod, ctx) => {
      const start = toNum(ctx.eval(expr[1], ctx));
      return expr.slice(2).reduce((acc, e) => util.mod(acc, toNum(ctx.eval(e, ctx))), start);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprMod>): ExpressionResult => {
      ctx.link(util.mod, 'mod');
      const params = ctx.operands.map((expr) => `(+(${expr})||0)`);
      let last: string = params[0];
      for (let i = 1; i < params.length; i++) last = `mod(${last}, ${params[i]})`;
      return new Expression(last);
    },
  ] as types.OperatorDefinition<types.ExprMod>,
 
  [
    'min',
    [],
    -1,
    (expr: types.ExprMin, ctx) => {
      return Math.min(...expr.slice(1).map((e) => toNum(ctx.eval(e, ctx))));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprMin>): ExpressionResult => {
      const params = ctx.operands.map((expr) => `(+(${expr})||0)`);
      return new Expression(`+Math.min(${params.join(',')})||0`);
    },
  ] as types.OperatorDefinition<types.ExprMin>,
 
  [
    'max',
    [],
    -1,
    (expr: types.ExprMax, ctx) => {
      return Math.max(...expr.slice(1).map((e) => toNum(ctx.eval(e, ctx))));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprMax>): ExpressionResult => {
      const params = ctx.operands.map((expr) => `(+(${expr})||0)`);
      return new Expression(`+Math.max(${params.join(',')})||0`);
    },
  ] as types.OperatorDefinition<types.ExprMax>,
 
  [
    'round',
    [],
    1,
    (expr: types.ExprRound, ctx) => {
      return Math.round(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprRound>): ExpressionResult => {
      return new Expression(`Math.round(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprRound>,
 
  [
    'ceil',
    [],
    1,
    (expr: types.ExprCeil, ctx) => {
      return Math.ceil(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprCeil>): ExpressionResult => {
      return new Expression(`Math.ceil(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprCeil>,
 
  [
    'floor',
    [],
    1,
    (expr: types.ExprFloor, ctx) => {
      return Math.floor(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprFloor>): ExpressionResult => {
      return new Expression(`Math.floor(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprFloor>,
 
  [
    'trunc',
    [],
    1,
    (expr: types.ExprTrunc, ctx) => {
      return Math.trunc(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprTrunc>): ExpressionResult => {
      return new Expression(`Math.trunc(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprTrunc>,
 
  [
    'abs',
    [],
    1,
    (expr: types.ExprAbs, ctx) => {
      return Math.abs(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprAbs>): ExpressionResult => {
      return new Expression(`Math.abs(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprAbs>,
 
  [
    'sqrt',
    [],
    1,
    (expr: types.ExprSqrt, ctx) => {
      return Math.sqrt(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprSqrt>): ExpressionResult => {
      return new Expression(`Math.sqrt(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprSqrt>,
 
  [
    'exp',
    [],
    1,
    (expr: types.ExprExp, ctx) => {
      return Math.exp(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprExp>): ExpressionResult => {
      return new Expression(`Math.exp(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprExp>,
 
  [
    'ln',
    [],
    1,
    (expr: types.ExprLn, ctx) => {
      return Math.log(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprLn>): ExpressionResult => {
      return new Expression(`Math.log(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprLn>,
 
  [
    'log',
    [],
    2,
    (expr: types.ExprLog, ctx) => {
      const num = toNum(ctx.eval(expr[1], ctx));
      const base = toNum(ctx.eval(expr[2], ctx));
      return Math.log(num) / Math.log(base);
    },
    (ctx: types.OperatorCodegenCtx<types.ExprLog>): ExpressionResult => {
      return new Expression(`Math.log(+(${ctx.operands[0]})||0)/Math.log(+(${ctx.operands[1]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprLog>,
 
  [
    'log10',
    [],
    1,
    (expr: types.ExprLog10, ctx) => {
      return Math.log10(toNum(ctx.eval(expr[1], ctx)));
    },
    (ctx: types.OperatorCodegenCtx<types.ExprLog10>): ExpressionResult => {
      return new Expression(`Math.log10(+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprLog10>,
 
  [
    '**',
    ['pow'],
    2,
    (expr: types.ExprPow, ctx) => {
      const num = toNum(ctx.eval(expr[1], ctx));
      const base = toNum(ctx.eval(expr[2], ctx));
      return num ** base;
    },
    (ctx: types.OperatorCodegenCtx<types.ExprPow>): ExpressionResult => {
      return new Expression(`Math.pow(+(${ctx.operands[0]})||0,+(${ctx.operands[0]})||0)`);
    },
  ] as types.OperatorDefinition<types.ExprPow>,
];