1
0
mirror of synced 2025-01-31 12:03:42 +01:00

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:
toby 2017-02-03 18:34:46 -05:00
parent 9bf0d66b88
commit b9f1cf968f

View File

@ -96,7 +96,9 @@ 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/);
input = Array.prototype.map.call(input, function(line) {
var words = line.split(/ +/);
words = Array.prototype.map.call(words, function(word) { words = Array.prototype.map.call(words, function(word) {
var letters = Array.prototype.map.call(word, function(character) { var letters = Array.prototype.map.call(word, function(character) {
var letter = character.toUpperCase(); var letter = character.toUpperCase();
@ -109,14 +111,24 @@ var MorseCode = {
return letters.join("<ld>"); return letters.join("<ld>");
}); });
line = words.join("<wd>");
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;
}, },