All files / src/shared/lib/variables variableNames.ts

100% Statements 32/32
100% Branches 12/12
100% Functions 8/8
100% Lines 24/24

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                  25x       92x 92x 13x   79x     79x     25x 93x             25x         104x         25x 207x             25x 36x     25x 70x     25x 45x     25x       137x 1x   136x 17x   119x    
import type { ScriptNormalized } from "shared/lib/entities/entitiesTypes";
import { Variable } from "shared/lib/resources/types";
 
type VariablesLookup = { [name: string]: Variable | undefined };
 
/******************************************************************************
 * Custom Event Variables
 */
 
export const customEventVariableName = (
  variable: string,
  customEvent: ScriptNormalized,
): string => {
  const customEventVariable = customEvent.variables[`V${variable}`];
  if (customEventVariable) {
    return customEventVariable.name;
  }
  const letter = String.fromCharCode(
    "A".charCodeAt(0) + parseInt(variable, 10),
  );
  return `Variable ${letter}`;
};
 
export const customEventVariableCode = (variable: string) => {
  return `V${variable}`;
};
 
/******************************************************************************
 * Local Variables
 */
 
export const localVariableName = (
  variable: string,
  entityId: string,
  variablesLookup: VariablesLookup,
) => {
  return (
    variablesLookup[`${entityId}__L${variable}`]?.name || `Local ${variable}`
  );
};
 
export const localVariableCode = (variable: string) => {
  return `L${variable}`;
};
 
/******************************************************************************
 * Temp Variables
 */
 
export const tempVariableName = (variable: string) => {
  return `Temp ${variable}`;
};
 
export const tempVariableCode = (variable: string) => {
  return `T${variable}`;
};
 
export const globalVariableCode = (variable: string) => {
  return variable.padStart(2, "0");
};
 
export const variableDisplayName = (
  name: string,
  arrayLength?: number | null,
): string => {
  if (arrayLength === null) {
    return `${name}[]`;
  }
  if (arrayLength !== undefined) {
    return `${name}[${arrayLength}]`;
  }
  return name;
};