All files / platform/core/src/utils buildButtonCommands.ts

100% Statements 17/17
100% Branches 7/7
100% Functions 4/4
100% Lines 15/15

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    60x 120x   60x         59x     59x 33x       59x 3x 3x 1x     2x 2x 2x               2x       59x    
import { ButtonProps, RunCommand } from '../types';
 
const toArray = <T>(value?: T | T[]): T[] =>
  Array.isArray(value) ? value : value != null ? [value] : [];
 
export const buildButtonCommands = (
  buttonProps: ButtonProps,
  baseArgs: Record<string, unknown>,
  { servicesManager, commandsManager }: AppTypes.Managers
): Array<() => unknown> => {
  const allCommands: Array<() => unknown> = [];
 
  // 1) normalize item-level commands
  for (const command of toArray(buttonProps.commands as RunCommand)) {
    allCommands.push(() => commandsManager.run(command, baseArgs));
  }
 
  // 2) normalize option-level commands
  for (const option of toArray(buttonProps.options)) {
    const shouldSkip = !option?.commands || option.explicitRunOnly;
    if (shouldSkip) {
      continue;
    }
 
    const valueToUse = option.value;
    for (const command of toArray(option.commands)) {
      const commandOptions = {
        ...option,
        value: valueToUse,
        options: buttonProps.options,
        servicesManager,
        commandsManager,
      };
 
      allCommands.push(() => commandsManager.run(command, commandOptions));
    }
  }
 
  return allCommands;
};