-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
215 lines (181 loc) · 5.85 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
let _AreaFrom, _AreaTo, _AlphabetFrom, _AlphabetTo, _BaseFrom, _BaseTo, _StartButton, _StatusBar, _AreaEncoded, _shuffleFrom, _shuffleTo, _decodeBtn, _encodeBtn
function init() {
_AreaFrom = document.getElementById("AreaFrom")
_AreaTo = document.getElementById("AreaTo")
_AlphabetFrom = document.getElementById("from")
_AlphabetTo = document.getElementById("to")
_BaseFrom = document.getElementById("fromS")
_BaseTo = document.getElementById("toS")
_StartButton = document.getElementById("start")
_StatusBar = document.getElementById("compress")
_AreaEncoded = document.getElementById("AreaEncoded")
_AutoButton = document.getElementById("autoFrom")
_decodeBtn = document.getElementById("decode")
_encodeBtn = document.getElementById("encode")
_AsciiFrom = document.getElementById("asciiFrom")
_AsciiTo = document.getElementById("asciiTo")
_ShuffleFrom = document.getElementById("shuffleFrom")
_ShuffleTo = document.getElementById("shuffleTo")
_SortFrom = document.getElementById("sortFrom")
_SortTo = document.getElementById("sortTo")
checkInputs()
}
const cleanInput = (x)=>(x
.replace(/\n\r?/g,'')
.replace(/\ /g,'␣')
.replace(/[\x00-\x1F]/g,(x)=>String.fromCharCode('␀'.charCodeAt(0)+x.charCodeAt(0)))
);
function onError(errorMessage) {
_StartButton.disabled = _ShuffleFrom.disabled = _ShuffleTo.disabled = true
_SortFrom.disabled = _SortTo.disabled = true
_StatusBar.value = errorMessage
_StatusBar.classList.add("statusErr")
return false
}
function noMoreError() {
_StartButton.disabled = _ShuffleFrom.disabled = _ShuffleTo.disabled = false
_SortFrom.disabled = _SortTo.disabled = false
_StatusBar.value = "Ready to convert"
_StatusBar.classList.remove("statusErr")
return true
}
function AreaEncodedChanged() {
_AreaEncoded.value = cleanInput(_AreaEncoded.value)
}
function autoAreaFrom() {
_AutoButton.disabled = true // avoid doubleclick
const bFrom = _AreaFrom.value
let alpha = _AlphabetFrom.value
for(let i=bFrom.length-1; i>=0; --i) {
const c = bFrom[i]
if(alpha.indexOf(c)<0)
alpha += c
}
_AlphabetFrom.value = alpha
checkInputs()
}
let checkingInputs = false;
function checkInputsAsync() {
if(!checkingInputs)
setTimeout(checkInputs, 1)
}
function checkInputs() {
checkingInputs = true
const alphaTo = (_AlphabetTo.value = cleanInput(_AlphabetTo.value))
const alphaFrom = (_AlphabetFrom.value = cleanInput(_AlphabetFrom.value))
const areaFrom = (_AreaFrom.value = cleanInput(_AreaFrom.value))
let error = false
let al = ""
for(let i=alphaTo.length-1;i>=0;--i) {
const c = alphaTo[i]
if(al.indexOf(c)>=0)
error = `Alphabet "To" contains duplicate symbols: '${c}' (charcode ${c.charCodeAt(0)}, at position ${i})`
al += c
}
if((_BaseTo.value = al.length) <= 1)
error = 'Alphabet "To" needs at least 2 characters'
al = ""
for(let i=alphaFrom.length-1;i>=0;--i) {
const c = alphaFrom[i]
if(al.indexOf(c)>=0)
error = `Alphabet "From" contains duplicate symbols: '${c}' (charcode ${c.charCodeAt(0)}, at position ${i})`
al += c
}
if((_BaseFrom.value = al.length) <= 1) {
_AutoButton.disabled = undefined
error = 'Alphabet "From" needs at least 2 characters'
}
for(let i=areaFrom.length-1; i>=0; --i) {
const c = areaFrom[i]
if(alphaFrom.indexOf(c)<0) {
_AutoButton.disabled = undefined
error = `Number "From" contains non-in-alphabet-characters: '${c}' (charcode ${c.charCodeAt(0)}, at position ${i})`
}
}
checkingInputs = false
if(error)
return onError(error);
_AutoButton.disabled = true
return noMoreError()
}
let run = false;
const antiDblClickStart = ()=>{_StartButton.disabled=undefined};
const doConvert = function(){
run = _AlphabetFrom.readOnly = _AlphabetTo.readOnly = _AreaFrom.readOnly = true
_encodeBtn.disabled = _decodeBtn.disabled = _ShuffleFrom.disabled = _ShuffleTo.disabled = true
d_start = new Date()
_StatusBar.value = `Started Compressing on ${d_start.toISOString()}...`
convert(_AlphabetFrom.value, _AlphabetTo.value, _AreaFrom.value, cancelConvert)
}
const cancelConvert = function(){
run = _AlphabetFrom.readOnly = _AlphabetTo.readOnly = _AreaFrom.readOnly = false
_encodeBtn.disabled = _decodeBtn.disabled = _ShuffleFrom.disabled = _ShuffleTo.disabled = undefined
}
function ClickStart() {
if(run) {// stop compress
// avoid doubleclick
_StartButton.disabled = true
setTimeout(antiDblClickStart,1000)
cancelConvert()
} else {
if(checkInputs()) { // check for errors before launching
// avoid doubleclick
_StartButton.disabled = true
doConvert()
setTimeout(antiDblClickStart,1000)
}
}
}
function ClickEncode() {
_AreaEncoded.value = encodeToStr(_AlphabetFrom.value, _AlphabetTo.value, _AreaTo.value)
}
function ClickDecode() {
const decoded = decodeFromStr(_AreaEncoded.value)
_AreaFrom.value = decoded.str
_AlphabetFrom.value = decoded.aFrom
_AlphabetTo.value = decoded.aTo
checkInputs()
}
const strSuffhle = (s)=>{
let a = s.split("")
for(let i = s.length-1; i>0; --i) {
const j = (Math.random()*(i+1))|0
const tmp = a[i]
a[i] = a[j]
a[j] = tmp
}
return a.join("")
}
const alphaAscii = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
function setAlphabetFrom(typeOfSet) {
setAlphabet(_AlphabetFrom, typeOfSet)
}
function setAlphabetTo(typeOfSet) {
setAlphabet(_AlphabetTo, typeOfSet)
}
function setAlphabet(input, typeOfSet) {
switch (typeOfSet) {
case 'shuffle':
input.value = strSuffhle(input.value)
break;
case 'sort':
input.value = input.value.split("").sort().join("")
break;
case 'ascii':
input.value = alphaAscii
break;
default:
console.log('Not defined "'+typeOfSet+'"')
return
}
checkInputsAsync()
}
function copyAlphabetFrominTo() {
let alphaTo = _AlphabetTo.value
let alphaFrom = _AlphabetFrom.value
for(let i=alphaFrom.length-1; i>=0; --i)
if(alphaTo.indexOf(alphaFrom[i])<0)
alphaTo+=alphaFrom[i]
_AlphabetTo.value = alphaTo
checkInputs()
}