src/modules/display/domain/displayHelpers.ts   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 52
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 7
mnd 3
bc 3
fnc 4
bpm 0.75
cpm 1.75
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A displayHelpers.ts ➔ generateWordAnnouncement 0 3 1
A displayHelpers.ts ➔ shouldBoxBeActive 0 3 1
A displayHelpers.ts ➔ shouldBoxBeDisabled 0 13 2
A displayHelpers.ts ➔ calculateDisplayState 0 22 3
1
export interface DisplayState {
2
  indexText: string;
3
  announcement: string;
4
  shouldGetWord: boolean;
5
}
6
7
export function calculateDisplayState(binaryValue: number): DisplayState {
8
  if (binaryValue === 0) {
9
    return {
10
      indexText: '-',
11
      announcement: 'No pattern selected',
12
      shouldGetWord: false,
13
    };
14
  }
15
16
  if (binaryValue > 2048) {
17
    return {
18
      indexText: binaryValue.toString(),
19
      announcement: `Value ${binaryValue} is out of range. Maximum is 2048`,
20
      shouldGetWord: false,
21
    };
22
  }
23
24
  return {
25
    indexText: binaryValue.toString(),
26
    announcement: '', // Will be filled with word info
27
    shouldGetWord: true,
28
  };
29
}
30
31
export function generateWordAnnouncement(word: string, index: number): string {
32
  return `Word selected: ${word}, index ${index}`;
33
}
34
35
export function shouldBoxBeActive(boxState: boolean): boolean {
36
  return boxState;
37
}
38
39
export function shouldBoxBeDisabled(index: number, boxes: boolean[]): boolean {
40
  const is2048Active = boxes[0];
41
  const isCurrentBoxActive = boxes[index];
42
43
  // Box 0 (2048) is disabled if any other box is active and 2048 is not active
44
  if (index === 0) {
45
    const isAnyOtherBoxActive = boxes.slice(1).some(box => box);
46
    return isAnyOtherBoxActive && !is2048Active;
47
  }
48
49
  // Other boxes are disabled if 2048 is active and the current box is not active
50
  return is2048Active && !isCurrentBoxActive;
51
}
52