added URI{Component,}{De,En}coder

This commit is contained in:
Manuel Friedli 2016-09-20 20:45:04 +02:00
parent 4197bca299
commit 5a1ca42f0d
6 changed files with 70 additions and 83 deletions

View file

@ -0,0 +1,15 @@
import {Converter} from "./converter";
export class URIComponentDecoder implements Converter {
getDisplayname():string {
return "Decode URI component";
}
getId():string {
return "uricomponentdecode";
}
convert(input:string):string {
return decodeURIComponent(input);
}
}

View file

@ -0,0 +1,17 @@
import {Converter} from "./converter";
export class URIComponentEncoder implements Converter {
getDisplayname():string {
return "Encode URI component";
}
getId():string {
return "uricomponentencode";
}
convert(input:string):string {
return encodeURIComponent(input).replace(/[!'()*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
}

View file

@ -0,0 +1,15 @@
import {Converter} from "./converter";
export class URIDecoder implements Converter {
getDisplayname():string {
return "Decode URI";
}
getId():string {
return "uridecode";
}
convert(input:string):string {
return decodeURI(input);
}
}

View file

@ -0,0 +1,15 @@
import {Converter} from "./converter";
export class URIEncoder implements Converter {
getDisplayname():string {
return "Encode URI";
}
getId():string {
return "uriencode";
}
convert(input:string):string {
return encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
}

View file

@ -2,6 +2,10 @@ import {Injectable} from "@angular/core";
import {Converter} from "./converter/converter"; import {Converter} from "./converter/converter";
import {Base64Encoder} from "./converter/base64encoder"; import {Base64Encoder} from "./converter/base64encoder";
import {Base64Decoder} from "./converter/base64decoder"; import {Base64Decoder} from "./converter/base64decoder";
import {URIEncoder} from "./converter/uriencoder";
import {URIDecoder} from "./converter/uridecoder";
import {URIComponentEncoder} from "./converter/uricomponentencoder";
import {URIComponentDecoder} from "./converter/uricomponentdecoder";
@Injectable() @Injectable()
export class ConverterregistryService { export class ConverterregistryService {
@ -27,6 +31,10 @@ export class ConverterregistryService {
private init():void { private init():void {
this.registerConverter(new Base64Encoder()); this.registerConverter(new Base64Encoder());
this.registerConverter(new Base64Decoder()); this.registerConverter(new Base64Decoder());
this.registerConverter(new URIEncoder());
this.registerConverter(new URIDecoder());
this.registerConverter(new URIComponentEncoder());
this.registerConverter(new URIComponentDecoder());
} }
private registerConverter(converter:Converter):void { private registerConverter(converter:Converter):void {

View file

@ -10,57 +10,6 @@
}; };
} }
}, },
{
"id": "base64decode",
"name": "Decode Base64",
"convert": function (input) {
try {
return {
"status": "OK",
"content": atob(input)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid base64 input string."
};
}
}
},
{
"id": "decodeuri",
"name": "Decode URI",
"convert": function (input) {
try {
return {
"status": "OK",
"content": decodeURI(input)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid URI input string."
};
}
}
},
{
"id": "decodeuricomponent",
"name": "Decode URI component",
"convert": function (input) {
try {
return {
"status": "OK",
"content": decodeURIComponent(input)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid URI component input string."
};
}
}
},
{ {
"id": "decodehtmlentities", "id": "decodehtmlentities",
"name": "Decode HTML entities", "name": "Decode HTML entities",
@ -133,38 +82,6 @@
} }
} }
}, },
{
"id": "base64encode",
"name": "Encode Base64",
"convert": function (input) {
return {
"status": "OK",
"content": btoa(input)
};
}
},
{
"id": "encodeuri",
"name": "Encode URI",
"convert": function (input) {
return {
"status": "OK",
"content": encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']')
};
}
},
{
"id": "encodeuricomponent",
"name": "Encode URI component",
"convert": function (input) {
return {
"status": "OK",
"content": encodeURIComponent(input).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
})
};
}
},
{ {
"id": "encodehtmlentities", "id": "encodehtmlentities",
"name": "Encode HTML entities", "name": "Encode HTML entities",