-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (100 loc) · 3.08 KB
/
app.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
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
108
(() => {
const $textInput = document.querySelector('#text-input');
const $output = document.querySelector('#output');
const $encriptButton = document.querySelector('#encript-button');
const $decriptButton = document.querySelector('#decript-button');
/**
* Function to reset output text and show default text
*/
const resetOutput = () => {
$output.innerHTML = `
<img
src="./public/not-found.svg"
alt="person with a magnifying glass looking at a diamond"
class="not-found" />
<h3> Ningún mensaje fue encontrado </h3>
<p class="sub-text">Ingresa el texto que desees encriptar o desencriptar.</p>
`
}
/**
*
* @param {string} text Input text to show in output with copy button
*/
const showOutput = (text) => {
$output.textContent = ''
const convertedContainer = document.createElement('div');
convertedContainer.className = 'converted-container'
const textElement = document.createElement('p');
textElement.className = 'converted'
textElement.textContent = text;
const copyButton = document.createElement('button');
copyButton.className = 'copy';
copyButton.textContent = 'Copiar';
copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(text)
})
convertedContainer.append(textElement, copyButton)
$output.appendChild(convertedContainer)
}
/**
* Function to encript text using specific algorithm
* keys = {e: enter,
* i: imes,
* a: ai,
* o: ober,
* u: ufat}
* @param {string} textToEncrypt text to encrypt
* @returns {string} encrypted text
*/
const encryptText = (textToEncrypt) => {
const keys = {
e: 'enter',
i: 'imes',
a: 'ai',
o: 'ober',
u: 'ufat'
}
for (const key in keys) {
textToEncrypt = textToEncrypt.replace(new RegExp(key, 'g'), keys[key])
}
return textToEncrypt;
}
/**
*
* @param {text} textToDecrypt
* @returns
*/
const decryptText = (textToDecrypt) => {
const keys = {
enter: 'e',
imes: 'i',
ai: 'a',
ober: 'o',
ufat: 'u'
}
for (const key in keys) {
textToDecrypt = textToDecrypt.replace(new RegExp(key, 'g'), keys[key])
}
return textToDecrypt;
}
/**
* Event to reset output text
*/
$encriptButton.addEventListener('click', () => {
const text = $textInput.value.trim()
if (text.length === 0) {
return resetOutput()
}
showOutput(encryptText(text))
})
/**
* Event to reset output text
*/
$decriptButton.addEventListener('click', () => {
const text = $textInput.value.trim()
if (text.length === 0) {
return resetOutput()
}
showOutput(decryptText(text))
})
})()