这并没什么实际作用,就是拿来玩玩。
如果你不知道morse code是用来做什么的,我想你总会知道电报吧,看文章封面你也就知道了。
Morse类的构建有四个参数
第一个参数是回调(callback)会把构建好的类本身返回来
众所周知,摩斯密码有长短之分
那么前两个参数分别是 摩斯长替代符和短替代符,最后一个参数是分隔符。
举例:
callback = (morse)=>{}, zfc = 'c', zfd = 'd', fg = ','
callback = (morse)=>{}, zfc = 'a', zfd = 'b', fg = '-'
我就不演示结果来,下面是代码
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Morse code (DE&EN) class</title>
<script src="morse.js"></script>
</head>
<body>
<div>encode('you become the meaning of my life')</div>
<div style="color:darkgray" id="v"></div>
<div style="margin-top: 5vh">decode('cdcc,ccc,ddc, ,cddd,d,cdcd,ccc,cc,d, ,c,dddd,d, ,cc,d,dc,cd,dd,cd,ccd, ,ccc,ddcd, ,cc,cdcc, ,dcdd,dd,ddcd,d')</div>
<div style="color:darkgray" id="s"></div>
<script>
new Morse((c) => {
let v = c.encode('you become the meaning of my life');
let s = c.decode('cdcc,ccc,ddc, ,cddd,d,cdcd,ccc,cc,d, ,c,dddd,d, ,cc,d,dc,cd,dd,cd,ccd, ,ccc,ddcd, ,cc,cdcc, ,dcdd,dd,ddcd,d');
console.log('en:', v);
console.log('de:', s);
document.querySelector('#v').innerHTML = v;
document.querySelector('#s').innerHTML = s;
});
</script>
</body>
</html>
js类:
class Morse {
zfc = '';//长
zfd = '';//短
fg = '';//分割
constructor(callback = null, zfc = 'c', zfd = 'd', fg = ',') {
this.zfc = zfc;
this.zfd = zfd;
this.fg = fg;
if (zfc === zfd) {
alert('长短符不能相同');
return;
}
if (callback) callback(this);
}
encode(string) {
let result = '';
for (let i in string) {
result += this.doMorseEN(string[i]) + this.fg;
//console.log(string[i], ' > ', this.doMorseEN(string[i]));
}
return result.substring(0, result.length - 1);//字母、数字编码
}
decode(string) {
let result = '';
const list = string.split(this.fg);
//console.log(list);
for (let i = 0; i < list.length; i++) {
result += this.doMorseDE(list[i]);
//console.log(list[i], ' > ', this.doMorseDE(list[i]));
}
return result;//字母、数字解码
}
doMorseEN(string) {
let result = '';
const c = this.zfc;
const d = this.zfd;
switch (string) {
case 'a':
result = d + c;
break;
case 'b':
result = c + d + d + d;
break;
case 'c':
result = c + d + c + d;
break;
case 'd':
result = c + d + d;
break;
case 'e':
result = d;
break;
case 'f':
result = d + d + c + d;
break;
case 'g':
result = c + c + d;
break;
case 'h':
result = d + d + d + d;
break;
case 'i':
result = d + d;
break;
case 'j':
result = d + c + c + c;
break;
case 'k':
result = c + d + c;
break;
case 'l':
result = d + c + d + d;
break;
case 'm':
result = c + c;
break;
case 'n':
result = c + d;
break;
case 'o':
result = c + c + c;
break;
case 'p':
result = d + c + c + d;
break;
case 'q':
result = c + c + d + c;
break;
case 'r':
result = d + c + d;
break;
case 's':
result = d + d + d;
break;
case 't':
result = c;
break;
case 'u':
result = d + d + c;
break;
case 'v':
result = d + d + d + c;
break;
case 'w':
result = d + c + c;
break;
case 'x':
result = c + d + d + c;
break;
case 'y':
result = c + d + c + c;
break;
case 'z':
result = c + c + d + d;
break;
case '1':
result = d + c + c + c + c;
break;
case '2':
result = d + d + c + c + c;
break;
case '3':
result = d + d + d + c + c;
break;
case '4':
result = d + d + d + d + c;
break;
case '5':
result = d + d + d + d + d;
break;
case '6':
result = c + d + d + d + d;
break;
case '7':
result = c + c + d + d + d;
break;
case '8':
result = c + c + c + d + d;
break;
case '9':
result = c + c + c + c + d;
break;
case '0':
result = c + c + c + c + c;
break;
default:
result = string;
break;
}
return result;
}
doMorseDE(string) {
let result = '';
const c = this.zfc;
const d = this.zfd;
switch (string) {
case (d+c):
result = 'a';
break;
case c + d + d + d:
result = 'b';
break;
case c + d + c + d:
result = 'c';
break;
case c + d + d:
result = 'd';
break;
case d:
result = 'e';
break;
case d + d + c + d:
result = 'f';
break;
case c + c + d:
result ='g';
break;
case d + d + d + d:
result = 'h';
break;
case d + d:
result = 'i';
break;
case d + c + c + c:
result = 'j';
break;
case c + d + c:
result = 'k';
break;
case d + c + d + d:
result = 'l';
break;
case c + c:
result = 'm';
break;
case c + d:
result = 'n';
break;
case c + c + c:
result = 'o';
break;
case d + c + c + d:
result = 'p';
break;
case c + c + d + c:
result = 'q';
break;
case d + c + d:
result = 'r';
break;
case d + d + d:
result = 's';
break;
case c:
result = 't';
break;
case d + d + c:
result = 'u';
break;
case d + d + d + c:
result = 'v';
break;
case d + c + c:
result = 'w';
break;
case c + d + d + c:
result = 'x';
break;
case c + d + c + c:
result = 'y';
break;
case c + c + d + d:
result = 'z';
break;
case d + c + c + c + c:
result = '1';
break;
case d + d + c + c + c:
result = '2';
break;
case d + d + d + c + c:
result = '3';
break;
case d + d + d + d + c:
result = '4';
break;
case d + d + d + d + d:
result = '5';
break;
case c + d + d + d + d:
result = '6';
break;
case c + c + d + d + d:
result = '7';
break;
case c + c + c + d + d:
result = '8';
break;
case c + c + c + c + d:
result = '9';
break;
case c + c + c + c + c:
result = '0';
break;
default:
result = string;
break;
}
return result;
}
}
这就是为什么我会戏称写代码的小朋友从”小码农“变成了”小蚂蚁“,这明明就是满屏的小蚂蚁。。。