/** * @author tlwr [toby@toby.codes] * @copyright Crown Copyright 2017 * @license Apache-2.0 */ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; import {LETTER_DELIM_OPTIONS, WORD_DELIM_OPTIONS} from "../lib/Delim.mjs"; /** * From Morse Code operation */ class FromMorseCode extends Operation { /** * FromMorseCode constructor */ constructor() { super(); this.name = "From Morse Code"; this.module = "Default"; this.description = "Translates Morse Code into (upper case) alphanumeric characters."; this.infoURL = "https://wikipedia.org/wiki/Morse_code"; this.inputType = "string"; this.outputType = "string"; this.args = [ { "name": "Letter delimiter", "type": "option", "value": LETTER_DELIM_OPTIONS }, { "name": "Word delimiter", "type": "option", "value": WORD_DELIM_OPTIONS } ]; this.checks = [ { pattern: "(?:^[-. \\n]{5,}$|^[_. \\n]{5,}$|^(?:dash|dot| |\\n){5,}$)", flags: "i", args: ["Space", "Line feed"] } ]; } /** * @param {string} input * @param {Object[]} args * @returns {string} */ run(input, args) { if (!this.reversedTable) { this.reverseTable(); } const letterDelim = Utils.charRep(args[0]); const wordDelim = Utils.charRep(args[1]); input = input.replace(/-|‐|−|_|–|—|dash/ig, ""); // hyphen-minus|hyphen|minus-sign|undersore|en-dash|em-dash input = input.replace(/\.|·|dot/ig, ""); let words = input.split(wordDelim); const self = this; words = Array.prototype.map.call(words, function(word) { const signals = word.split(letterDelim); const letters = signals.map(function(signal) { return self.reversedTable[signal]; }); return letters.join(""); }); words = words.join(" "); return words; } /** * Reverses the Morse Code lookup table */ reverseTable() { this.reversedTable = {}; for (const letter in MORSE_TABLE) { const signal = MORSE_TABLE[letter]; this.reversedTable[signal] = letter; } } } 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 FromMorseCode;