Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
morseCode.ts
Go to the documentation of this file.
1
88import { logger, LogType } from "./logger";
89import { getMessage } from "./messageProvider";
90
102export class MorseTranslator {
104 private static readonly log: LogType = logger;
105
119 private static readonly MORSE_CODE: Record<string, string> = {
120 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..',
121 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
122 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
123 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
124 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
125 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
126 'Y': '-.--', 'Z': '--..',
127 '0': '-----', '1': '.----', '2': '..---', '3': '...--',
128 '4': '....-', '5': '.....', '6': '-....', '7': '--...',
129 '8': '---..', '9': '----.',
130 '.': '.-.-.-', ',': '--..--', '?': '..--..',
131 "'": '.----.', '!': '-.-.--', '/': '-..-.',
132 '(': '-.--.', ')': '-.--.-', '&': '.-...',
133 ':': '---...', ';': '-.-.-.', '=': '-...-',
134 '+': '.-.-.', '-': '-....-', '_': '..--.-',
135 '"': '.-..-.', '$': '...-..-', '@': '.--.-.',
136 ' ': '/', // space between words
137 '\n': '//' // end of sentence or line
138 };
139
149 private static readonly REVERSE_MORSE_CODE: Record<string, string> = (() => {
150 const rev: Record<string, string> = {};
151 for (const [char, code] of Object.entries(MorseTranslator.MORSE_CODE)) {
152 rev[code] = char;
153 }
154 return rev;
155 })();
156
182 public static toMorse(input: string): string {
183 logger.debug(getMessage("inFunction", "toMorse", "MorseTranslator"));
184 const final: string = input
185 .toUpperCase()
186 .split('')
187 .map(char => MorseTranslator.MORSE_CODE[char] ?? '')
188 .join(' ');
189 this.log.info(getMessage("morseConverted", input, final));
190 return final;
191 }
192
223 public static fromMorse(morseInput: string): string {
224 logger.debug(getMessage("inFunction", "fromMorse", "MorseTranslator"));
225 const decoded = morseInput
226 .split(' ')
227 .map(symbol => MorseTranslator.REVERSE_MORSE_CODE[symbol] ?? '')
228 .join('')
229 .replace(/\/\//g, '\n')
230 .replace(/\//g, ' ');
231
232 this.log.info(getMessage("morseDecoded", morseInput, decoded));
233 return decoded;
234 }
235}
Static utility class for Morse code translation operations.
Definition morseCode.ts:102
export const logger
Singleton logger instance providing unified logging interface for the entire extension.
Definition logger.ts:910
export type LogType
Type alias for Log class enabling dependency injection and testing.
Definition logger.ts:940
export const getMessage
Exported function for direct message retrieval.