-
Notifications
You must be signed in to change notification settings - Fork 0
/
encodeDecode.js
47 lines (39 loc) · 1.17 KB
/
encodeDecode.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
function decodeFromStr(string) {
// string should be like: abcdaefghefghefghefgh
// abcd a efgh efgheghegh
// origin separator code encoded
// origin alphabet: any char, all differents
// separator: first char of origin
// code alphabet: any char, all differents
// encoded: any char composed by code alphabet
const aOrigin = []
const aCode = []
// get origin alphabet
let index=-1
const len = string.length
while(true) {
if(++index > len) {
alert("string do not follow the right format")
return // TODO ERROR
}
const curr = string[index]
if(aOrigin[0]===curr)
break // found from/to separator char
aOrigin.push(curr)
}
// get code alphabet (index is separator char)
while(true) {
if(++index > len) {
alert("string do not follow the right format")
return // TODO ERROR
}
const curr = string[index]
if(aCode.indexOf(curr)>0)
break // found first char from encoded string
aCode.push(curr)
}
return {aFrom:aCode.join(""), aTo:aOrigin.join(""), str:string.substr(index)}
}
function encodeToStr(alFrom, alDest, encodedString) {
return alFrom + alFrom[0] + alDest + encodedString
}