src/modules/theme/domain/themeLogic.ts   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.57

Size

Lines of Code 36
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 11
mnd 4
bc 4
fnc 7
bpm 0.5714
cpm 1.5713
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A themeLogic.ts ➔ isValidTheme 0 3 1
A themeLogic.ts ➔ toggleTheme 0 3 2
A themeLogic.ts ➔ getAriaPressed 0 3 2
A themeLogic.ts ➔ getThemeToggleLabel 0 4 1
A themeLogic.ts ➔ getOppositeThemeName 0 3 2
A themeLogic.ts ➔ isDarkMode 0 3 1
A themeLogic.ts ➔ getThemeOrDefault 0 6 2
1
export type Theme = 'light' | 'dark';
2
3
export const DEFAULT_THEME: Theme = 'dark';
4
5
export function toggleTheme(current: string | null): Theme {
6
  return current === 'dark' ? 'light' : 'dark';
7
}
8
9
export function isValidTheme(theme: string): theme is Theme {
10
  return theme === 'light' || theme === 'dark';
11
}
12
13
export function getThemeOrDefault(theme: string | null): Theme {
14
  if (theme && isValidTheme(theme)) {
15
    return theme;
16
  }
17
  return DEFAULT_THEME;
18
}
19
20
export function isDarkMode(theme: string): boolean {
21
  return theme === 'dark';
22
}
23
24
export function getOppositeThemeName(theme: string): string {
25
  return theme === 'dark' ? 'light' : 'dark';
26
}
27
28
export function getAriaPressed(theme: string): 'true' | 'false' {
29
  return isDarkMode(theme) ? 'true' : 'false';
30
}
31
32
export function getThemeToggleLabel(currentTheme: string): string {
33
  const opposite = getOppositeThemeName(currentTheme);
34
  return `Switch to ${opposite} mode`;
35
}
36