2017-03-23 17:52:20 +00:00
|
|
|
|
import Utils from "../Utils.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-03 16:43:30 -05:00
|
|
|
|
/**
|
|
|
|
|
* Morse Code translation operations.
|
|
|
|
|
*
|
|
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
|
* @copyright Crown Copyright 2017
|
|
|
|
|
* @license Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
|
const MorseCode = {
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constant
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
FORMAT_OPTIONS: ["-/.", "_/.", "Dash/Dot", "DASH/DOT", "dash/dot"],
|
|
|
|
|
/**
|
|
|
|
|
* @constant
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
2017-02-07 14:37:26 +00:00
|
|
|
|
LETTER_DELIM_OPTIONS: ["Space", "Line feed", "CRLF", "Forward slash", "Backslash", "Comma", "Semi-colon", "Colon"],
|
2017-02-03 16:43:30 -05:00
|
|
|
|
/**
|
|
|
|
|
* @constant
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
2017-02-07 14:37:26 +00:00
|
|
|
|
WORD_DELIM_OPTIONS: ["Line feed", "CRLF", "Forward slash", "Backslash", "Comma", "Semi-colon", "Colon"],
|
2017-02-03 16:43:30 -05:00
|
|
|
|
/**
|
|
|
|
|
* @constant
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
MORSE_TABLE: {
|
|
|
|
|
"A": "<dot><dash>",
|
|
|
|
|
"B": "<dash><dot><dot><dot>",
|
|
|
|
|
"C": "<dash><dot><dash><dot>",
|
|
|
|
|
"D": "<dash><dot><dot>",
|
|
|
|
|
"E": "<dot>",
|
|
|
|
|
"F": "<dot><dot><dash><dot>",
|
|
|
|
|
"G": "<dash><dash><dot>",
|
|
|
|
|
"H": "<dot><dot><dot><dot>",
|
|
|
|
|
"I": "<dot><dot>",
|
|
|
|
|
"J": "<dot><dash><dash><dash>",
|
|
|
|
|
"K": "<dash><dot><dash>",
|
|
|
|
|
"L": "<dot><dash><dot><dot>",
|
|
|
|
|
"M": "<dash><dash>",
|
|
|
|
|
"N": "<dash><dot>",
|
|
|
|
|
"O": "<dash><dash><dash>",
|
|
|
|
|
"P": "<dot><dash><dash><dot>",
|
|
|
|
|
"Q": "<dash><dash><dot><dash>",
|
|
|
|
|
"R": "<dot><dash><dot>",
|
|
|
|
|
"S": "<dot><dot><dot>",
|
|
|
|
|
"T": "<dash>",
|
|
|
|
|
"U": "<dot><dot><dash>",
|
|
|
|
|
"V": "<dot><dot><dot><dash>",
|
|
|
|
|
"W": "<dot><dash><dash>",
|
|
|
|
|
"X": "<dash><dot><dot><dash>",
|
|
|
|
|
"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>",
|
2017-02-09 17:20:06 +07:00
|
|
|
|
".": "<dot><dash><dot><dash><dot><dash>",
|
|
|
|
|
",": "<dash><dash><dot><dot><dash><dash>",
|
|
|
|
|
":": "<dash><dash><dash><dot><dot><dot>",
|
|
|
|
|
";": "<dash><dot><dash><dot><dash><dot>",
|
|
|
|
|
"!": "<dash><dot><dash><dot><dash><dash>",
|
|
|
|
|
"?": "<dot><dot><dash><dash><dot><dot>",
|
|
|
|
|
"'": "<dot><dash><dash><dash><dash><dot>",
|
|
|
|
|
"\"": "<dot><dash><dot><dot><dash><dot>",
|
|
|
|
|
"/": "<dash><dot><dot><dash><dot>",
|
|
|
|
|
"-": "<dash><dot><dot><dot><dot><dash>",
|
|
|
|
|
"+": "<dot><dash><dot><dash><dot>",
|
|
|
|
|
"(": "<dash><dot><dash><dash><dot>",
|
|
|
|
|
")": "<dash><dot><dash><dash><dot><dash>",
|
|
|
|
|
"@": "<dot><dash><dash><dot><dash><dot>",
|
|
|
|
|
"=": "<dash><dot><dot><dot><dash>",
|
|
|
|
|
"&": "<dot><dash><dot><dot><dot>",
|
|
|
|
|
"_": "<dot><dot><dash><dash><dot><dash>",
|
|
|
|
|
"$": "<dot><dot><dot><dash><dot><dot><dash>"
|
2017-02-03 16:43:30 -05:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To Morse Code operation.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} input
|
|
|
|
|
* @param {Object[]} args
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
2017-02-07 14:20:34 +00:00
|
|
|
|
runTo: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const format = args[0].split("/");
|
|
|
|
|
const dash = format[0];
|
|
|
|
|
const dot = format[1];
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const letterDelim = Utils.charRep[args[1]];
|
|
|
|
|
const wordDelim = Utils.charRep[args[2]];
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-03 18:34:46 -05:00
|
|
|
|
input = input.split(/\r?\n/);
|
|
|
|
|
input = Array.prototype.map.call(input, function(line) {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
let words = line.split(/ +/);
|
2017-02-03 18:34:46 -05:00
|
|
|
|
words = Array.prototype.map.call(words, function(word) {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const letters = Array.prototype.map.call(word, function(character) {
|
|
|
|
|
const letter = character.toUpperCase();
|
2017-02-09 15:09:33 +00:00
|
|
|
|
if (typeof MorseCode.MORSE_TABLE[letter] == "undefined") {
|
2017-02-03 18:34:46 -05:00
|
|
|
|
return "";
|
|
|
|
|
}
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-03 18:34:46 -05:00
|
|
|
|
return MorseCode.MORSE_TABLE[letter];
|
|
|
|
|
});
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-03 18:34:46 -05:00
|
|
|
|
return letters.join("<ld>");
|
|
|
|
|
});
|
|
|
|
|
line = words.join("<wd>");
|
|
|
|
|
return line;
|
2017-02-03 16:43:30 -05:00
|
|
|
|
});
|
2017-02-03 18:34:46 -05:00
|
|
|
|
input = input.join("\n");
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-03 18:34:46 -05:00
|
|
|
|
input = input.replace(
|
|
|
|
|
/<dash>|<dot>|<ld>|<wd>/g,
|
|
|
|
|
function(match) {
|
2017-02-09 15:09:33 +00:00
|
|
|
|
switch (match) {
|
2017-02-03 18:34:46 -05:00
|
|
|
|
case "<dash>": return dash;
|
|
|
|
|
case "<dot>": return dot;
|
2017-02-04 00:17:04 +00:00
|
|
|
|
case "<ld>": return letterDelim;
|
|
|
|
|
case "<wd>": return wordDelim;
|
2017-02-03 18:34:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-03 18:34:46 -05:00
|
|
|
|
return input;
|
2017-02-03 16:43:30 -05:00
|
|
|
|
},
|
2017-02-04 00:17:04 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-03 16:43:30 -05:00
|
|
|
|
/**
|
|
|
|
|
* From Morse Code operation.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} input
|
|
|
|
|
* @param {Object[]} args
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
2017-02-07 14:20:34 +00:00
|
|
|
|
runFrom: (function() {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
let reversedTable = null;
|
|
|
|
|
const reverseTable = function() {
|
2017-02-03 16:43:30 -05:00
|
|
|
|
reversedTable = {};
|
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
|
for (const letter in MorseCode.MORSE_TABLE) {
|
|
|
|
|
const signal = MorseCode.MORSE_TABLE[letter];
|
2017-02-03 16:43:30 -05:00
|
|
|
|
reversedTable[signal] = letter;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return function(input, args) {
|
2017-02-09 15:09:33 +00:00
|
|
|
|
if (reversedTable === null) {
|
2017-02-03 16:43:30 -05:00
|
|
|
|
reverseTable();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const letterDelim = Utils.charRep[args[0]];
|
|
|
|
|
const wordDelim = Utils.charRep[args[1]];
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-02-09 17:29:12 +07:00
|
|
|
|
input = input.replace(/-|‐|−|_|–|—|dash/ig, "<dash>"); //hyphen-minus|hyphen|minus-sign|undersore|en-dash|em-dash
|
|
|
|
|
input = input.replace(/\.|·|dot/ig, "<dot>");
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
|
let words = input.split(wordDelim);
|
2017-02-03 16:43:30 -05:00
|
|
|
|
words = Array.prototype.map.call(words, function(word) {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const signals = word.split(letterDelim);
|
2017-02-03 16:43:30 -05:00
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
|
const letters = signals.map(function(signal) {
|
2017-02-03 16:43:30 -05:00
|
|
|
|
return reversedTable[signal];
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-04 00:17:04 +00:00
|
|
|
|
return letters.join("");
|
2017-02-03 16:43:30 -05:00
|
|
|
|
});
|
|
|
|
|
words = words.join(" ");
|
|
|
|
|
|
|
|
|
|
return words;
|
|
|
|
|
};
|
|
|
|
|
})(),
|
2017-02-04 00:17:04 +00:00
|
|
|
|
|
2017-02-03 16:43:30 -05:00
|
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
|
|
export default MorseCode;
|