-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (97 loc) · 2.46 KB
/
script.js
File metadata and controls
107 lines (97 loc) · 2.46 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const letters = {
q: "й",
w: "ц",
e: "у",
r: "к",
t: "е",
y: "н",
u: "г",
i: "ш",
o: "щ",
p: "з",
"[": "х",
"]": "ї",
a: "ф",
s: "і",
d: "в",
f: "а",
g: "п",
h: "р",
j: "о",
k: "л",
l: "д",
";": "ж",
"'": "є",
z: "я",
x: "ч",
c: "с",
v: "м",
b: "и",
n: "т",
m: "ь",
",": "б",
".": "ю",
"?": ",",
"/": ".",
};
// function for adding exceptions to objects(in case there are more languages in the future)
function addException(object, key, value) {
object[key] = value;
}
//create object with upper-case elements
const capitalLetters = Object.fromEntries(
Object.entries(letters).map(([key, value]) => [
key.toUpperCase(),
value.toUpperCase(),
])
);
addException(capitalLetters, "<", "Б");
addException(capitalLetters, ">", "Ю");
addException(capitalLetters, "{", "Х");
addException(capitalLetters, "}", "Ї");
addException(capitalLetters, ":", "Ж");
addException(capitalLetters, '"', "Є");
//create objects with swapped keys and values
function swapObject(obj) {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
}
const swappedLetters = swapObject(letters);
const swappedCapitalLetters = swapObject(capitalLetters);
//merge objects for better iteration
const initialObjects = { ...capitalLetters, ...letters };
const swappedObjects = { ...swappedCapitalLetters, ...swappedLetters };
//for outputting the result
const inputText = document.querySelector("#textarea");
const outputText = document.querySelector(".output");
function converter() {
const input = inputText.value;
outputText.value = "";
const output = [...input].map(char =>
initialObjects[char] ?? swappedObjects[char] ?? char
);
outputText.value = output.join("");
}
//function to clear input
function clear() {
inputText.value = "";
outputText.value = "";
}
//function to copy the result
function copy() {
const copyInput = outputText.value;
if (copyInput) {
navigator.clipboard.writeText(copyInput);
copyHint();
}
}
//function that appears when the result is copied
function copyHint() {
const hint = document.querySelector(".hint");
hint.classList.add("show-hint");
setTimeout(function () {
hint.classList.remove("show-hint");
}, 1200);
}
document.querySelector(".b-convert").addEventListener("click", converter);
document.querySelector(".b-clear").addEventListener("click", clear);
document.querySelector(".b-copy").addEventListener("click", copy);