Passed
Push — master ( 5f67fe...8bce0b )
by Jesús
02:16
created

src/modules/wordInput/domain/wordInputHelpers.ts   A

Complexity

Total Complexity 13
Complexity/F 1.44

Size

Lines of Code 46
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 13
mnd 4
bc 4
fnc 9
bpm 0.4444
cpm 1.4443
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A wordInputHelpers.ts ➔ isValidIndex 0 3 1
A wordInputHelpers.ts ➔ getWordByIndex 0 6 2
A wordInputHelpers.ts ➔ binaryValueToIndex 0 3 1
A wordInputHelpers.ts ➔ getSuggestions 0 11 2
A wordInputHelpers.ts ➔ shouldShowSuggestions 0 3 1
A wordInputHelpers.ts ➔ indexToBinaryValue 0 3 1
A wordInputHelpers.ts ➔ getWordIndex 0 3 1
1
export function isWordInWordlist(word: string, wordlist: string[]): boolean {
2
  const normalizedWord = word.trim().toLowerCase();
3
  if (!normalizedWord) return false;
4
  
5
  return wordlist.some(w => w.toLowerCase() === normalizedWord);
6
}
7
8
export function getSuggestions(input: string, wordlist: string[], maxSuggestions: number = 10): string[] {
9
  const normalizedInput = input.trim().toLowerCase();
10
  
11
  if (!normalizedInput) {
12
    return [];
13
  }
14
15
  return wordlist
16
    .filter(word => word.toLowerCase().startsWith(normalizedInput))
17
    .slice(0, maxSuggestions);
18
}
19
20
export function getWordIndex(word: string, wordlist: string[]): number {
21
  return wordlist.indexOf(word);
22
}
23
24
export function indexToBinaryValue(index: number): number {
25
  return index + 1;
26
}
27
28
export function binaryValueToIndex(value: number): number {
29
  return value - 1;
30
}
31
32
export function isValidIndex(index: number, wordlistLength: number): boolean {
33
  return index >= 0 && index < wordlistLength;
34
}
35
36
export function getWordByIndex(index: number, wordlist: string[]): string | null {
37
  if (!isValidIndex(index, wordlist.length)) {
38
    return null;
39
  }
40
  return wordlist[index];
41
}
42
43
export function shouldShowSuggestions(input: string, minLength: number = 1): boolean {
44
  return input.trim().length >= minLength;
45
}
46