All files / src/shared/lib/scriptValue format.ts

79.01% Statements 64/81
80.56% Branches 58/72
50% Functions 2/4
78.48% Lines 62/79

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                    43x       43x       62x 1x   61x 28x 33x 1x 32x   32x 1x 31x 1x     30x 1x   2x         29x 5x 24x 3x 21x 2x       19x 1x       18x 2x       16x 2x       14x         14x         14x         14x         14x         14x         14x         14x         14x         14x 1x       13x 1x       12x 2x 10x         10x 1x       9x 1x       8x 1x       7x 1x       6x 1x 5x 2x 3x 1x 2x 1x       1x 1x                  
import { ScriptValue } from "./types";
 
interface ScriptValueToStringOptions {
  variableNameForId: (value: string) => string;
  constantNameForId: (value: string) => string;
  actorNameForId: (value: string) => string;
  propertyNameForId: (value: string) => string;
  directionForValue: (value: string) => string;
}
 
export const assertUnreachable = (_x: never): never => {
  throw new Error("Didn't expect to get here");
};
 
export const scriptValueToString = (
  value: ScriptValue | undefined,
  options: ScriptValueToStringOptions
): string => {
  if (!value) {
    return "0";
  }
  if (value.type === "number") {
    return String(value.value);
  } else if (value.type === "variable") {
    return options.variableNameForId(value.value);
  } else Iif (value.type === "constant") {
    return options.constantNameForId(value.value);
  } else if (value.type === "direction") {
    return options.directionForValue(value.value);
  } else if (value.type === "property") {
    return `${options.actorNameForId(value.target)}.${options.propertyNameForId(
      value.property
    )}`;
  } else if (value.type === "expression") {
    return String(value.value || "0")
      .replace(/\$([VLT]*[0-9]+)\$/g, (_, match) => {
        return options.variableNameForId(match);
      })
      .replace(/@([a-z0-9-]{36})@/g, (_, match) => {
        return `||constant:${match}||`;
      });
  } else if (value.type === "true") {
    return "true";
  } else if (value.type === "false") {
    return "false";
  } else if (value.type === "add") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} + ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "sub") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} - ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "mul") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} * ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "div") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} / ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "mod") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} % ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "gt") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} > ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "gte") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} >= ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "lt") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} < ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "lte") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} <= ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "eq") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} == ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "ne") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} != ${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "min") {
    return `min(${scriptValueToString(
      value.valueA,
      options
    )},${scriptValueToString(value.valueB, options)})`;
  } else Iif (value.type === "max") {
    return `max(${scriptValueToString(
      value.valueA,
      options
    )},${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "and") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} && ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "or") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} || ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "not") {
    return `!(${scriptValueToString(value.value, options)})`;
  } else Iif (value.type === "shl") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} << ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "shr") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} >> ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "bAND") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} & ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "bOR") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} | ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "bXOR") {
    return `(${scriptValueToString(
      value.valueA,
      options
    )} ^ ${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "bNOT") {
    return `~(${scriptValueToString(value.value, options)})`;
  } else if (value.type === "abs") {
    return `abs(${scriptValueToString(value.value, options)})`;
  } else if (value.type === "isqrt") {
    return `isqrt(${scriptValueToString(value.value, options)})`;
  } else if (value.type === "atan2") {
    return `atan2(${scriptValueToString(
      value.valueA,
      options
    )},${scriptValueToString(value.valueB, options)})`;
  } else if (value.type === "rnd") {
    return `rnd(${scriptValueToString(value.value, options)})`;
  } else IEif (value.type === "indirect") {
    return `INDIRECT`;
  }
 
  assertUnreachable(value.type);
 
  return "";
};