Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
baudot.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
20export class BaudotCipher extends BaseCipher {
24 readonly CipherName = "Baudot";
25
29 private readonly letters: Record<string, string>;
30
34 private readonly inverseLetters: Record<string, string>;
35
42 super();
43
44 this.letters = {
45 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..',
46 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
47 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
48 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
49 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
50 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
51 'Y': '-.--', 'Z': '--..'
52 };
53
54 this.inverseLetters = {};
55 for (const letter in this.letters) {
56 this.inverseLetters[this.letters[letter]] = letter;
57 }
58 }
59
67 encode(plainText: string = ""): string {
68 const encodedTokens: string[] = [];
69
70 for (const ch of plainText) {
71 const token = this.letters[ch.toUpperCase()] ?? ch;
72 encodedTokens.push(token);
73 }
74
75 return encodedTokens.join(" ");
76 }
77
85 decode(cipherText: string = ""): string {
86 const decodedTokens: string[] = [];
87
88 const tokens = cipherText.split(" ");
89 for (const token of tokens) {
90 decodedTokens.push(this.inverseLetters[token] ?? token);
91 }
92
93 return decodedTokens.join("");
94 }
95}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of Baudot telegraph code encoding.
Definition baudot.ts:20
readonly CipherName
Identifier name for this cipher.
Definition baudot.ts:24
constructor()
Constructor for Baudot code cipher.
Definition baudot.ts:41
export const Record< string,(...args:any[])=> string