Repository: gchq/CyberChef
File: src/js/operations/MorseCode.js (JavaScript)

Code before:
        "Y": "<dash><dot><dash><dash>",
        "Z": "<dash><dash><dot><dot>",
        "1": "<dot><dash><dash><dash><dash>",
        "2": "<dot><dot><dash><dash><dash>",
        "3": "<dot><dot><dot><dash><dash>",
        "4": "<dot><dot><dot><dot><dash>",
        "5": "<dot><dot><dot><dot><dot>",
        "6": "<dash><dot><dot><dot><dot>",
        "7": "<dash><dash><dot><dot><dot>",
        "8": "<dash><dash><dash><dot><dot>",
        "9": "<dash><dash><dash><dash><dot>",
        "0": "<dash><dash><dash><dash><dash>",
    },


    /**
     * To Morse Code operation.
     *
     * @param {number} input
     * @param {Object[]} args
     * @returns {string}
     */
    translateTo: function(input, args) {
        var format = args[0].split("/");
        var dash = format[0];
        var dot = format[1];

        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 "";
                }

                return MorseCode.MORSE_TABLE[letter];
            });

            return letters.join("<ld>");
        });

        var morse = words.join("<wd>");
        morse = morse.replace(/<dash>/g, dash);
        morse = morse.replace(/<dot>/g, dot);
        morse = morse.replace(/<ld>/g, letter_delim);
        morse = morse.replace(/<wd>/g, word_delim);

        return morse;

Review comment (suggestion):
Better to only use `var` once, and seperate each line with a comma like this:
```js
var format = args[0].split("/"),
    dash = format[0],
    dot = format[1];
```
Follows the style of the rest of the app.

Code after:
        "Y": "<dash><dot><dash><dash>",
        "Z": "<dash><dash><dot><dot>",
        "1": "<dot><dash><dash><dash><dash>",
        "2": "<dot><dot><dash><dash><dash>",
        "3": "<dot><dot><dot><dash><dash>",
        "4": "<dot><dot><dot><dot><dash>",
        "5": "<dot><dot><dot><dot><dot>",
        "6": "<dash><dot><dot><dot><dot>",
        "7": "<dash><dash><dot><dot><dot>",
        "8": "<dash><dash><dash><dot><dot>",
        "9": "<dash><dash><dash><dash><dot>",
        "0": "<dash><dash><dash><dash><dash>",
    },


    /**
     * To Morse Code operation.
     *
     * @param {number} input
     * @param {Object[]} args
     * @returns {string}
     */
    translateTo: function(input, args) {
        var format = args[0].split("/");
        var dash = format[0];
        var dot = format[1];

        var letterDelim = MorseCode.OPTION_TABLE[args[1]];
        var wordDelim = MorseCode.OPTION_TABLE[args[2]];

        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 letters.join("<ld>");
            });
            line = words.join("<wd>");
            return line;
        });
        input = input.join("\n");

        input = input.replace(