JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
'use strict';
var MorseCode = function MorseCode() {
this.DOT = 'dot';
this.DASH = 'dash';
this.SPACE = 'space';
this.map = {
'a': [
this.DOT,
this.DASH
],
'b': [
this.DASH,
this.DOT,
this.DOT,
this.DOT
],
'c': [
this.DASH,
this.DOT,
this.DASH,
this.DOT
],
'd': [
this.DASH,
this.DOT,
this.DOT
],
'e': [this.DOT],
'f': [
this.DOT,
this.DOT,
this.DASH,
this.DOT
],
'g': [
this.DASH,
this.DASH,
this.DAT
],
'h': [
this.DOT,
this.DOT,
this.DOT,
this.DOT
],
'i': [
this.DOT,
this.DOT
],
'j': [
this.DOT,
this.DASH,
this.DASH,
this.DASH
],
'k': [
this.DASH,
this.DOT,
this.DASH
],
'l': [
this.DOT,
this.DASH,
this.DOT,
this.DOT
],
'm': [
this.DASH,
this.DASH
],
'n': [
this.DASH,
this.DOT
],
'o': [
this.DASH,
this.DASH,
this.DASH
],
'p': [
this.DOT,
this.DASH,
this.DASH,
this.DOT
],
'q': [
this.DASH,
this.DASH,
this.DOT,
this.DASH
],
'r': [
this.DOT,
this.DASH,
this.DOT
],
's': [
this.DOT,
this.DOT,
this.DOT
],
't': [this.DASH],
'u': [
this.DOT,
this.DOT,
this.DASH,
this.DASH
],
'v': [
this.DOT,
this.DOT,
this.DOT,
this.DASH
],
'w': [
this.DOT,
this.DASH,
this.DASH
],
'x': [
this.DASH,
this.DOT,
this.DOT,
this.DASH
],
'y': [
this.DASH,
this.DOT,
this.DASH,
this.DASH
],
'z': [
this.DASH,
this.DASH,
this.DOT,
this.DOT
],
' ': [this.SPACE],
'1': [
this.DOT,
this.DASH,
this.DASH,
this.DASH,
this.DASH
],
'2': [
this.DOT,
this.DOT,
this.DASH,
this.DASH,
this.DASH
],
'3': [
this.DOT,
this.DOT,
this.DOT,
this.DASH,
this.DASH
],
'4': [
this.DOT,
this.DOT,
this.DOT,
this.DOT,
this.DASH
],
'5': [
this.DOT,
this.DOT,
this.DOT,
this.DOT,
this.DOT
],
'6': [
this.DASH,
this.DOT,
this.DOT,
this.DOT,
this.DOT
],
'7': [
this.DASH,
this.DASH,
this.DOT,
this.DOT,
this.DOT
],
'8': [
this.DASH,
this.DASH,
this.DASH,
this.DOT,
this.DOT
],
'9': [
this.DASH,
this.DASH,
this.DASH,
this.DASH,
this.DOT
],
'0': [
this.DASH,
this.DASH,
this.DASH,
this.DASH,
this.DASH
],
'.': [
this.DOT,
this.DASH,
this.DOT,
this.DASH,
this.DOT,
this.DASH
],
',': [
this.DASH,
this.DASH,
this.DOT,
this.DOT,
this.DASH,
this.DASH
],
'\'': [
this.DASH,
this.DOT,
this.DASH,
this.DOT,
this.DASH,
this.DOT
],
'!': [
this.DASH,
this.DOT,
this.DASH,
this.DOT,
this.DASH,
this.DASH
],
'-': [
this.DASH,
this.DOT,
this.DOT,
this.DOT,
this.DOT,
this.DASH
],
'&': [
this.DOT,
this.DASH,
this.DOT,
this.DOT,
this.DOT
],
'?': [
this.DOT,
this.DOT,
this.DASH,
this.DASH,
this.DOT,
this.DOT
],
'/': [
this.DASH,
this.DOT,
this.DOT,
this.DASH,
this.DOT
],
'(': [
this.DASH,
this.DOT,
this.DASH,
this.DASH,
this.DOT
],
')': [
this.DASH,
this.DOT,
this.DASH,
this.DASH,
this.DOT,
this.DASH
],
':': [
this.DASH,
this.DASH,
this.DASH,
this.DOT,
this.DOT,
this.DOT
],
';': [
this.DASH,
this.DOT,
this.DASH,
this.DOT,
this.DASH,
this.DOT
],
'=': [
this.DASH,
this.DOT,
this.DOT,
this.DOT,
this.DASH
],
'+': [
this.DOT,
this.DASH,
this.DOT,
this.DASH,
this.DOT
],
'_': [
this.DOT,
this.DOT,
this.DASH,
this.DASH,
this.DOT,
this.DASH
],
'"': [
this.DOT,
this.DASH,
this.DOT,
this.DOT,
this.DASH,
this.DOT
],
'$': [
this.DOT,
this.DOT,
this.DOT,
this.DASH,
this.DOT,
this.DOT,
this.DASH
],
'@': [
this.DOT,
this.DASH,
this.DASH,
this.DOT,
this.DASH,
this.DOT
]
};
};
$traceurRuntime.createClass(MorseCode, {
parse: function(input) {
input = input.split('');
for (var i = 0, ii = input.length; i < ii; i++) {
input[i] = {
original: input[i].toLowerCase(),
sequence: this.map[input[i].toLowerCase()]
};
}
console.log(input);
return input;
},
humanReadable: function(input) {
var text = ' ';
input = this.parse(input);
for (var i = 0, ii = input.length; i < ii; i++) {
var current = input[i];
for (var c = 0, cc = current.sequence.length; c < cc; c++) {
var char;
switch (current.sequence[c]) {
case this.SPACE:
char = '\n';
break;
case this.DOT:
char = '\u2022';
break;
case this.DASH:
char = '\u2013';
}
text += char;
}
text += ' ';
}
console.log(text);
return text;
}
}, {});
var morseCode = new MorseCode();
var input = $('textarea.input');
var run = $('button.run');
var output = $('pre.output');
var clear = $('button.clear');
run.on('click', function(e) {
var inputText = input.val();
console.log('run');
output.text(morseCode.humanReadable(inputText));
});
clear.on('click', function(e) {
input.val('');
output.text('');
});