-
Notifications
You must be signed in to change notification settings - Fork 0
/
countWords.js
47 lines (44 loc) · 1.45 KB
/
countWords.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
47
chrome.tabs.executeScript({
code: "window.getSelection().toString();"
}, selection => {
let data;
if ((selection && selection[0] === "") || !selection) {
data = "No text selected."
} else {
data = computeStats(selection[0])
loadData(data)
}
});
function computeStats(text) {
const counter = count(text);
return `<p className="data">${counter("chars")} characters</p>
<p className="data">${counter("alphabets")} characters (A-Z)</p>
<p className="data">${counter("words")} words</p>
<p className="data">${counter("sents")} sentences</p>
<p className="data">${counter("paras")} paragraphs</p>
`
}
function count(text) {
return (metric) => {
switch (metric) {
case "chars":
return text.replace("\n", "").length;
case "alphabets":
return text.replace(/[^A-Za-z]/g, "").length;
case "words":
return text.split(/\s+\b/g).length;
case "sents":
return text.split(/\.+\s+/g).length
case "paras":
return text.split(/\.+\n+/g).length
default:
throw Error('Provide a valid metric from ["chars", "words", "sents", "paras"]')
}
}
}
function loadData(data) {
const elem = document.querySelector(".text p")
const errorElem = document.querySelector(".empty");
errorElem.style.display = "none";
elem.innerHTML = data;
}