first commit

This commit is contained in:
2025-11-21 13:03:06 +01:00
commit 043812606d
8 changed files with 1206 additions and 0 deletions

95
morse.ts Normal file
View File

@@ -0,0 +1,95 @@
// Morse code translation utilities
export const MORSE_CODE: Record<string, string> = {
'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': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
' ': '/',
'.': '.-.-.-',
',': '--..--',
'?': '..--..',
'!': '-.-.--',
'-': '-....-',
'/': '-..-.',
'@': '.--.-.',
'(': '-.--.',
')': '-.--.-'
};
// Reverse mapping for morse to text
export const MORSE_TO_TEXT: Record<string, string> = Object.fromEntries(
Object.entries(MORSE_CODE).map(([key, value]) => [value, key])
);
/**
* Convert text to morse code
*/
export function textToMorse(text: string): string {
return text
.toUpperCase()
.split('')
.map(char => MORSE_CODE[char] || char)
.join(' ');
}
/**
* Convert morse code to text
*/
export function morseToText(morse: string): string {
return morse
.split(' ')
.map(code => MORSE_TO_TEXT[code] || code)
.join('');
}
/**
* Normalize morse code input (remove extra spaces, normalize separators)
*/
export function normalizeMorse(input: string): string {
return input
.trim()
.replace(/\s+/g, ' ')
.replace(/\//g, ' / ');
}
/**
* Compare two morse code strings with tolerance for spacing differences
*/
export function compareMorse(expected: string, actual: string): boolean {
const normalizedExpected = normalizeMorse(expected);
const normalizedActual = normalizeMorse(actual);
return normalizedExpected === normalizedActual;
}