diff --git a/src/js/operations/MorseCode.js b/src/js/operations/MorseCode.js index 8001a152..a9cab2d6 100644 --- a/src/js/operations/MorseCode.js +++ b/src/js/operations/MorseCode.js @@ -96,27 +96,39 @@ var MorseCode = { var letter_delim = MorseCode.OPTION_TABLE[args[1]]; var word_delim = MorseCode.OPTION_TABLE[args[2]]; - var words = input.split(/ +/); - words = Array.prototype.map.call(words, function(word) { - var letters = Array.prototype.map.call(word, function(character) { - var letter = character.toUpperCase(); - if(typeof MorseCode.MORSE_TABLE[letter] == "undefined") { - return ""; - } + 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) { + var letters = Array.prototype.map.call(word, function(character) { + 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(""); }); - - return letters.join(""); + line = words.join(""); + return line; }); + input = input.join("\n"); - var morse = words.join(""); - morse = morse.replace(//g, dash); - morse = morse.replace(//g, dot); - morse = morse.replace(//g, letter_delim); - morse = morse.replace(//g, word_delim); + input = input.replace( + /|||/g, + function(match) { + switch(match) { + case "": return dash; + case "": return dot; + case "": return letter_delim; + case "": return word_delim; + } + } + ); - return morse; + return input; },