forked from flowernal/slovnik-slovenskeho-jazyka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (65 loc) · 1.82 KB
/
script.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Prepne všetky checkboxy
* @param {HTMLInputElement} source
*/
const toggleAll = source => {
let checkboxes = document.getElementsByName("flag");
for (let checkbox of checkboxes)
checkbox.checked = source.checked;
}
const exportFile = () => {
let checkboxes = document.getElementsByName("flag");
let flags = [];
let includeWordsWithoutFlags = false;
for (let checkbox of checkboxes) {
if (checkbox.checked) {
if (checkbox.value === "0") {
includeWordsWithoutFlags = true;
}
else {
flags.push(checkbox.value);
}
}
}
fetch("slovnik.txt")
.then(data => data.text())
.then(text => {
const minLength = document.getElementById("minLength");
const maxLength = document.getElementById("maxLength");
const removeUppercase = document.getElementById("removeUppercase").checked;
let words = text.split("\n").filter(word => {
const split = word.split("/");
if (includeWordsWithoutFlags && !split[1]) {
return true;
}
else if (split[1]) {
for (let flag of flags) {
if (split[1].includes(flag)) {
return true;
}
}
}
return false;
}).map(word => word.split("/")[0]).filter(word => {
if (minLength.value.length && word.length < Number(minLength.value)) {
return false;
}
if (maxLength.value.length && word.length > Number(maxLength.value)) {
return false;
}
if (removeUppercase && word.toLowerCase() !== word) {
return false;
}
return true;
});
let element = document.createElement("a");
element.setAttribute("href",
"data:text/plain;charset=utf-8," + encodeURIComponent(words.join("\n"))
);
element.setAttribute("download", "exported.txt");
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
});
}