added the ConverterregistryService and two converters (base64 de- and

encoder)
This commit is contained in:
Manuel Friedli 2016-09-15 20:43:37 +02:00
parent dd4ffd9989
commit b30c698546
6 changed files with 94 additions and 12 deletions

View file

@ -1,19 +1,26 @@
import {Component} from "@angular/core"; import {Component} from "@angular/core";
import {OnInit}from "@angular/core";
import {InputareaComponent} from "./inputarea.component"; import {ConverterregistryService} from "./converterregistry.service";
// import {ConversionType} from "./conversiontype"; import {Converter} from "./converter";
//import { SelectorComponent } from "./selector.component";
@Component({ @Component({
moduleId: module.id, moduleId: module.id,
selector: "den-app", selector: "den-app",
templateUrl: "app.component.html", templateUrl: "app.component.html",
styleUrls: ["app.component.css"] styleUrls: ["app.component.css"],
providers: [ConverterregistryService]
}) })
export class AppComponent { export class AppComponent extends OnInit {
// private inputAreas:InputareaComponent[] = []; constructor(private converterregistryService:ConverterregistryService) {
// super();
// constructor() { }
// this.inputAreas.push(new InputareaComponent());
// } ngOnInit():void {
let converters:Converter[] = this.converterregistryService.getConverters();
console.log("Number of registered converters: " + converters.length);
for (let i = 0; i < converters.length; i++) {
console.log("Converter " + converters[i].getId() + ": " + converters[i].getDisplayname());
}
}
} }

15
app/base64decoder.ts Normal file
View file

@ -0,0 +1,15 @@
import {Converter} from './converter';
export class Base64Decoder implements Converter {
getDisplayname():string {
return "Decode Base 64";
}
getId():string {
return "base64decode";
}
convert(input:string):string {
return atob(input);
}
}

15
app/base64encoder.ts Normal file
View file

@ -0,0 +1,15 @@
import {Converter} from './converter';
export class Base64Encoder implements Converter {
getDisplayname():string {
return "Encode Base 64";
}
getId():string {
return "base64encode";
}
convert(input:string):string {
return btoa(input);
}
}

5
app/converter.ts Normal file
View file

@ -0,0 +1,5 @@
export interface Converter {
getDisplayname():string;
getId():string;
convert(input:string):string;
}

View file

@ -0,0 +1,41 @@
import {Injectable} from '@angular/core';
import {Converter} from './converter';
import {Base64Encoder} from "./base64encoder";
import {Base64Decoder} from "./base64decoder";
@Injectable()
export class ConverterregistryService {
private converters:Converter[] = [];
constructor() {
this.init();
}
public getConverters():Converter[] {
return this.converters;
}
public getConverter(id:string):Converter {
for (let i = 0; i < this.converters.length; i++) {
if (this.converters[i].getId() == id) {
return this.converters[i];
}
}
return undefined;
}
private init():void {
this.registerConverter(new Base64Encoder());
this.registerConverter(new Base64Decoder());
}
private registerConverter(converter:Converter):void {
this.converters.forEach((c:Converter) => {
if (c.getId() == converter.getId()) {
throw new Error("Converter-ID " + converter.getId() + " is already registered!");
}
});
this.converters.push(converter);
}
}

View file

@ -2,5 +2,4 @@ import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {AppModule} from "./app.module"; import {AppModule} from "./app.module";
const platform = platformBrowserDynamic(); platformBrowserDynamic().bootstrapModule(AppModule);
platform.bootstrapModule(AppModule);