chore: claude code refactor (7.5/10)
This commit is contained in:
31
morse.ts
31
morse.ts
@@ -54,24 +54,51 @@ export const MORSE_TO_TEXT: Record<string, string> = Object.fromEntries(
|
||||
Object.entries(MORSE_CODE).map(([key, value]) => [value, key])
|
||||
);
|
||||
|
||||
/**
|
||||
* Check if a character is supported in Morse code
|
||||
*/
|
||||
export function isCharacterSupported(char: string): boolean {
|
||||
return char.toUpperCase() in MORSE_CODE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if Morse code string is properly formatted
|
||||
*/
|
||||
export function isValidMorse(input: string): boolean {
|
||||
// Valid morse contains only dots, dashes, spaces, and forward slashes
|
||||
return /^[.\-\s/]*$/.test(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert text to morse code
|
||||
* Unsupported characters are silently skipped
|
||||
*/
|
||||
export function textToMorse(text: string): string {
|
||||
return text
|
||||
.toUpperCase()
|
||||
.split('')
|
||||
.map(char => MORSE_CODE[char] || char)
|
||||
.map(char => MORSE_CODE[char])
|
||||
.filter(code => code !== undefined)
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert morse code to text
|
||||
* Invalid morse codes are replaced with '?'
|
||||
*/
|
||||
export function morseToText(morse: string): string {
|
||||
if (!isValidMorse(morse)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return morse
|
||||
.split(' ')
|
||||
.map(code => MORSE_TO_TEXT[code] || code)
|
||||
.map(code => {
|
||||
// Handle empty strings (multiple spaces)
|
||||
if (code === '') return '';
|
||||
// Return the decoded character or '?' for unknown codes
|
||||
return MORSE_TO_TEXT[code] || '?';
|
||||
})
|
||||
.join('');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user