Make translateTo faster and aware of line breaks
Speed improvement is due to using a function regex instead of multiple find/replace calls.
This commit is contained in:
parent
9bf0d66b88
commit
b9f1cf968f
@ -96,27 +96,39 @@ var MorseCode = {
|
|||||||
var letter_delim = MorseCode.OPTION_TABLE[args[1]];
|
var letter_delim = MorseCode.OPTION_TABLE[args[1]];
|
||||||
var word_delim = MorseCode.OPTION_TABLE[args[2]];
|
var word_delim = MorseCode.OPTION_TABLE[args[2]];
|
||||||
|
|
||||||
var words = input.split(/ +/);
|
input = input.split(/\r?\n/);
|
||||||
words = Array.prototype.map.call(words, function(word) {
|
input = Array.prototype.map.call(input, function(line) {
|
||||||
var letters = Array.prototype.map.call(word, function(character) {
|
var words = line.split(/ +/);
|
||||||
var letter = character.toUpperCase();
|
words = Array.prototype.map.call(words, function(word) {
|
||||||
if(typeof MorseCode.MORSE_TABLE[letter] == "undefined") {
|
var letters = Array.prototype.map.call(word, function(character) {
|
||||||
return "";
|
var letter = character.toUpperCase();
|
||||||
}
|
if(typeof MorseCode.MORSE_TABLE[letter] == "undefined") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
return MorseCode.MORSE_TABLE[letter];
|
return MorseCode.MORSE_TABLE[letter];
|
||||||
|
});
|
||||||
|
|
||||||
|
return letters.join("<ld>");
|
||||||
});
|
});
|
||||||
|
line = words.join("<wd>");
|
||||||
return letters.join("<ld>");
|
return line;
|
||||||
});
|
});
|
||||||
|
input = input.join("\n");
|
||||||
|
|
||||||
var morse = words.join("<wd>");
|
input = input.replace(
|
||||||
morse = morse.replace(/<dash>/g, dash);
|
/<dash>|<dot>|<ld>|<wd>/g,
|
||||||
morse = morse.replace(/<dot>/g, dot);
|
function(match) {
|
||||||
morse = morse.replace(/<ld>/g, letter_delim);
|
switch(match) {
|
||||||
morse = morse.replace(/<wd>/g, word_delim);
|
case "<dash>": return dash;
|
||||||
|
case "<dot>": return dot;
|
||||||
|
case "<ld>": return letter_delim;
|
||||||
|
case "<wd>": return word_delim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return morse;
|
return input;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user