26 lines
720 B
TypeScript
26 lines
720 B
TypeScript
import {Converter} from './converter';
|
|
|
|
export class ROT13Converter implements Converter {
|
|
getDisplayname(): string {
|
|
return 'Perform ROT-13';
|
|
}
|
|
|
|
getId(): string {
|
|
return 'rot13';
|
|
}
|
|
|
|
convert(input: string): string {
|
|
try {
|
|
const inChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
const outChars = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
|
|
const translate = c => {
|
|
const charIndex = inChars.indexOf(c);
|
|
return charIndex > -1 ? outChars[charIndex] : c;
|
|
};
|
|
return input.split('').map(translate).join('');
|
|
} catch (exception) {
|
|
throw new Error('Could not perform ROT-13 conversion. Maybe corrupt input?');
|
|
}
|
|
}
|
|
}
|