All files / src/lib/compiler helpers.ts

85% Statements 17/20
42.86% Branches 3/7
66.67% Functions 2/3
81.25% Lines 13/16

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            8x             8x   8x                     8x 6x 6x 6x 6x         6x             6x     8x 1x   8x    
export type DirectionGBVMValue =
  | "DIR_DOWN"
  | "DIR_LEFT"
  | "DIR_RIGHT"
  | "DIR_UP";
 
const DIR_ENUM_LOOKUP: Record<string, DirectionGBVMValue> = {
  down: "DIR_DOWN",
  left: "DIR_LEFT",
  right: "DIR_RIGHT",
  up: "DIR_UP",
} as const;
 
const TEXT_SPEED_LOOKUP = [0x0, 0x1, 0x3, 0x7, 0xf, 0x1f] as const;
 
const KEY_BITS: Record<string, number> = {
  left: 0x02,
  right: 0x01,
  up: 0x04,
  down: 0x08,
  a: 0x10,
  b: 0x20,
  select: 0x40,
  start: 0x80,
} as const;
 
export const inputDec = (input: string | string[]) => {
  let output = 0;
  if (Array.isArray(input)) {
    for (let i = 0; i < input.length; i++) {
      output |= KEY_BITS[input[i]];
    }
  } else E{
    output = KEY_BITS[input];
  }
  Iif (output === 0) {
    // If no input set game would hang
    // as could not continue on, assume
    // this isn't what user wants and
    // instead allow any input
    output = 255;
  }
  return output;
};
 
export const dirEnum = (dir: unknown): DirectionGBVMValue =>
  DIR_ENUM_LOOKUP[dir as string] || "DIR_DOWN";
 
export const textSpeedDec = (speed: number): number =>
  TEXT_SPEED_LOOKUP[speed] !== undefined ? TEXT_SPEED_LOOKUP[speed] : 0x1;