This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
146 lines (130 loc) · 3.25 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;(function() {
const entrada = document.querySelector('#entrada')
const barra = document.querySelector('.barra-lateral')
var salida
function crearSalida() {
const elem = document.querySelector('#salida')
const child = document.createTextNode('')
elem.innerHTML = '' //para borrar todo los childNodes
elem.appendChild(child) //para hacer del textNode el único hijo
// trabajamos la salida como textNode para evitar basura
// al hacer node.textContent (además de que node.value es más rápido)
salida = child
}
crearSalida()
// exportaciones
window.uiCodificar = uiCodificar
window.uiDecodificar = uiDecodificar
window.uiCopiar = uiCopiar
entrada.oninput = uiSoloLetras
function Cod(x) {
switch(x) {
case 'e': return 'enter'
case 'i': return 'imes'
case 'a': return 'ai'
case 'o': return 'ober'
case 'u': return 'ufat'
default : return x
}
}
function codificar(s) {
var r = ''
for (const c of s) {
r += Cod(c)
}
return r
}
function error() {
throw new SyntaxError('codificación inválida')
}
function decodificar(s) {
var r = ''
for (var j = 0; j < s.length;) {
switch(s[j]) {
case 'e':
if (s[j + 4] === 'r') { r += s[j]; j += 5 }
else { error() }
break
case 'i':
if (s[j + 3] === 's') { r += s[j]; j += 4 }
else { error() }
break
case 'a':
if (s[j + 1] === 'i') { r += s[j]; j += 2 }
else { error()}
break
case 'o':
if (s[j + 3] === 'r') { r += s[j]; j += 4 }
else { error() }
break
case 'u':
if (s[j + 3] === 't') { r += s[j]; j += 4 }
else { error() }
break
default:
r += s[j++]
}
}
return r
}
function mostrarResultado() {
barra.classList.add('con-salida')
}
function ocultarResultado() {
barra.classList.remove('con-salida')
}
const kUnAllowed = /[^a-z ]/g
function uiSoloLetras(ev) {
const { inputType, target, data } = ev
// caso más frecuente
if (inputType === 'insertText') {
kUnAllowed.lastIndex = 0
if (kUnAllowed.test(data)) {
let value = target.value
target.value = value.substring(0, value.length - 1)
alert('solo letras minúsculas y sin acentos')
}
} else if(inputType === 'insertFromPaste') {
let value = data || target.value || ''
value = value.toLowerCase()
target.value = value.replace(kUnAllowed, '')
if (target.value !== value) {
alert('se ha modificado el texto para coincidir con los carácteres permitidos')
}
}
}
function uiDecodificar() {
var txt = entrada.value
entrada.value = ''
if (txt.length === 0) {
salida.nodeValue = ''
ocultarResultado()
} else {
try {
salida.nodeValue = decodificar(txt)
} catch(O_o) {
salida.nodeValue = 'Error: no se pudo decodificar la cadena, porque no es una codificación válida'
}
mostrarResultado()
}
}
function uiCodificar() {
var txt = entrada.value
entrada.value = ''
if (txt.length === 0) {
salida.nodeValue = ''
ocultarResultado()
} else {
salida.nodeValue = codificar(txt)
mostrarResultado()
}
}
const kClipboard = navigator.clipboard
function uiCopiar() {
if (kClipboard) {
kClipboard
.writeText(salida.nodeValue)
.then(() => alert('copiado'))
}
}
}())