All files / src/shared/lib/rpn helpers.ts

82% Statements 41/50
56.25% Branches 18/32
100% Functions 9/9
78.05% Lines 32/41

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 10346x 46x                   46x 233x             46x 44x 1x   43x 1x   42x     46x 131x     46x 189x     46x 69x     46x 21x     46x 28x     28x 19x     5x       4x     10x                                                   9x     46x 25x       6x   19x     46x 16x    
import { assertUnreachable } from "shared/lib/helpers/assert";
import {
  Associativity,
  FunctionSymbol,
  functionSymbols,
  OperatorSymbol,
  operatorSymbols,
  functionArgsLen,
  Token,
} from "./types";
 
export const isNumeric = (str: string) => {
  return (
    str.toLowerCase() === "true" ||
    str.toLowerCase() === "false" ||
    (!isNaN(Number(str)) && isFinite(Number(str)))
  );
};
 
export const toNumber = (str: string) => {
  if (str.toLowerCase() === "true") {
    return 1;
  }
  if (str.toLowerCase() === "false") {
    return 0;
  }
  return Number(str);
};
 
export const isOperatorSymbol = (x: string): x is OperatorSymbol => {
  return (operatorSymbols as unknown as string[]).indexOf(x) > -1;
};
 
export const isFunctionSymbol = (x: string): x is FunctionSymbol => {
  return (functionSymbols as unknown as string[]).indexOf(x) > -1;
};
 
export const isVariable = (token: string): boolean => {
  return !!/^[$A-Z_][0-9A-Z_$]*$/i.exec(token);
};
 
export const isConstant = (token: string): boolean => {
  return !!/^@([a-z0-9-]{36}|engine::[^@]+)@$/i.exec(token);
};
 
export const getPrecedence = (token: Token): number => {
  Iif (token.type === "FUN") {
    return 4;
  }
  if (token.type === "OP") {
    switch (token.operator) {
      case "~":
      case "!":
        return 14;
      case "*":
      case "/":
      case "%":
        return 12;
      case "+":
      case "-":
        return 11;
      case "<<":
      case ">>":
        return 10;
      case "<":
      case ">":
      case ">=":
      case "<=":
        return 9;
      case "==":
      case "!=":
        return 8;
      case "&":
        return 7;
      case "^":
        return 6;
      case "|":
        return 5;
      case "&&":
        return 4;
      case "||":
        return 3;
    }
    /* istanbul ignore next */
    assertUnreachable(token.operator);
  }
  return -1;
};
 
export const getAssociativity = (token: Token): Associativity => {
  if (
    token.type === "OP" &&
    (token.operator === "!" || token.operator === "~")
  ) {
    return Associativity.Right;
  }
  return Associativity.Left;
};
 
export const getArgsLen = (symbol: FunctionSymbol) => {
  return functionArgsLen[symbol];
};