Add ROT-13 conversion #3

Merged
manuel merged 1 commit from feature/rot-13 into develop 2018-08-31 22:00:36 +02:00
2 changed files with 27 additions and 0 deletions
Showing only changes of commit ed87b42839 - Show all commits

View file

@ -0,0 +1,25 @@
import {Converter} from './converter';
export class ROT13Converter implements Converter {
getDisplayname(): string {
return 'Perform ROT-13';
}
getId(): string {
return 'rot13convert';
}
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?');
}
}
}

View file

@ -19,6 +19,7 @@ import {PunycodeEncoder} from './converter/punycodeencoder';
import {PunycodeDecoder} from './converter/punycodedecoder'; import {PunycodeDecoder} from './converter/punycodedecoder';
import {UTF8Encoder} from './converter/utf8encoder'; import {UTF8Encoder} from './converter/utf8encoder';
import {UTF8Decoder} from './converter/utf8decoder'; import {UTF8Decoder} from './converter/utf8decoder';
import {ROT13Converter} from './converter/rot13converter';
@Injectable() @Injectable()
export class ConverterRegistryService { export class ConverterRegistryService {
@ -60,6 +61,7 @@ export class ConverterRegistryService {
this.registerConverter(new PunycodeDecoder(this.wrapper)); this.registerConverter(new PunycodeDecoder(this.wrapper));
this.registerConverter(new UTF8Encoder(this.wrapper)); this.registerConverter(new UTF8Encoder(this.wrapper));
this.registerConverter(new UTF8Decoder(this.wrapper)); this.registerConverter(new UTF8Decoder(this.wrapper));
this.registerConverter(new ROT13Converter());
} }
private registerConverter(converter: Converter): void { private registerConverter(converter: Converter): void {