forked from Laboratoria/SAP012-text-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.js
47 lines (40 loc) · 1.25 KB
/
analyzer.js
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
const analyzer = {
getCharacterCount: (text) => {
return text.length;
},
getWordCount: (text) => {
// Separa em palavras
const splitWords = text.split(/\s+/);
// Filtro para remover espaços e números
const wordsNoSpacesNoNumbers = splitWords.filter(word => word !== ''&& !/\d/.test(word));
// Retorna apenas a contagem de palavras
return wordsNoSpacesNoNumbers.length;
},
getCharacterCountExcludingSpaces: (text) => {
const cleanText = text.replace(/[^\w\s]/g, "").replace(/\s+/g, "");
return cleanText.length;
},
getNumberCount: (text) => {
const isThatANum = text.match(/\b\d+(\.\d+)?\b/g);
return isThatANum ? isThatANum.length : 0;
},
getNumberSum: (text) => {
const numberSum = text.match(/\b\d+(\.\d+)?\b/g);
if (numberSum) {
let total = 0;
for (let i = 0; i < numberSum.length; i++) {
total += Number(numberSum[i]);
}
return total;
} else {
return 0;
}
},
getAverageWordLength: (text) => {
const words = text.trim().split(/\s+/);
const characterTotal = words.reduce((acc, word) => acc + word.length, 0);
const average = characterTotal / words.length || 0;
return parseFloat(average.toFixed(2));
},
};
export default analyzer;