/**
* @author tlwr [toby@toby.codes]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
import Operation from "../Operation";
import Utils from "../Utils";
import {LETTER_DELIM_OPTIONS, WORD_DELIM_OPTIONS} from "../lib/Delim";
/**
* To Morse Code operation
*/
class ToMorseCode extends Operation {
/**
* ToMorseCode constructor
*/
constructor() {
super();
this.name = "To Morse Code";
this.module = "Default";
this.description = "Translates alphanumeric characters into International Morse Code.
Ignores non-Morse characters.
e.g. SOS
becomes ... --- ...
";
this.infoURL = "https://wikipedia.org/wiki/Morse_code";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Format options",
"type": "option",
"value": ["-/.", "_/.", "Dash/Dot", "DASH/DOT", "dash/dot"]
},
{
"name": "Letter delimiter",
"type": "option",
"value": LETTER_DELIM_OPTIONS
},
{
"name": "Word delimiter",
"type": "option",
"value": WORD_DELIM_OPTIONS
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const format = args[0].split("/");
const dash = format[0];
const dot = format[1];
const letterDelim = Utils.charRep(args[1]);
const wordDelim = Utils.charRep(args[2]);
input = input.split(/\r?\n/);
input = Array.prototype.map.call(input, function(line) {
let words = line.split(/ +/);
words = Array.prototype.map.call(words, function(word) {
const letters = Array.prototype.map.call(word, function(character) {
const letter = character.toUpperCase();
if (typeof MORSE_TABLE[letter] == "undefined") {
return "";
}
return MORSE_TABLE[letter];
});
return letters.join("");
});
line = words.join("");
return line;
});
input = input.join("\n");
input = input.replace(
/|||/g,
function(match) {
switch (match) {
case "": return dash;
case "": return dot;
case "": return letterDelim;
case "": return wordDelim;
}
}
);
return input;
}
}
const MORSE_TABLE = {
"A": "",
"B": "",
"C": "",
"D": "",
"E": "",
"F": "",
"G": "",
"H": "",
"I": "",
"J": "",
"K": "",
"L": "",
"M": "",
"N": "",
"O": "",
"P": "",
"Q": "",
"R": "",
"S": "",
"T": "",
"U": "",
"V": "",
"W": "",
"X": "",
"Y": "",
"Z": "",
"1": "",
"2": "",
"3": "",
"4": "",
"5": "",
"6": "",
"7": "",
"8": "",
"9": "",
"0": "",
".": "",
",": "",
":": "",
";": "",
"!": "",
"?": "",
"'": "",
"\"": "",
"/": "",
"-": "",
"+": "",
"(": "",
")": "",
"@": "",
"=": "",
"&": "",
"_": "",
"$": ""
};
export default ToMorseCode;