added 6 more converters:

- dec <-> bin
- dec <-> hex
- htmlentities de- and encode
This commit is contained in:
Manuel Friedli 2016-09-20 21:12:13 +02:00
parent 5a1ca42f0d
commit 01df7ac29c
8 changed files with 106 additions and 103 deletions

View File

@ -0,0 +1,14 @@
import {Converter} from "./converter";
export class BinToDecConverter implements Converter {
getDisplayname():string {
return "Convert binary to decimal";
}
getId():string {
return "bintodec";
}
convert(input:string):string {
return parseInt(input, 2).toString(10);
}
}

View File

@ -0,0 +1,14 @@
import {Converter} from "./converter";
export class DecToBinConverter implements Converter {
getDisplayname():string {
return "Convert decimal to binary";
}
getId():string {
return "dectobin";
}
convert(input:string):string {
return parseInt(input, 10).toString(2);
}
}

View File

@ -0,0 +1,14 @@
import {Converter} from "./converter";
export class DecToHexConverter implements Converter {
getDisplayname():string {
return "Convert decimal to heximal";
}
getId():string {
return "dectohex";
}
convert(input:string):string {
return parseInt(input, 10).toString(16);
}
}

View File

@ -0,0 +1,14 @@
import {Converter} from "./converter";
export class HexToDecConverter implements Converter {
getDisplayname():string {
return "Convert heximal to decimal";
}
getId():string {
return "hextodec";
}
convert(input:string):string {
return parseInt(input, 16).toString(10);
}
}

View File

@ -0,0 +1,19 @@
import {Converter} from "./converter";
export class HTMLEntitiesDecoder implements Converter {
getDisplayname():string {
return "Decode HTML entities";
}
getId():string {
return "decodehtmlentities";
}
convert(input:string):string {
return input
.replace(/\&quot\;/g, "\"")
.replace(/\&gt\;/g, ">")
.replace(/\&lt\;/g, "<")
.replace(/\&amp\;/g, "&");
}
}

View File

@ -0,0 +1,19 @@
import {Converter} from "./converter";
export class HTMLEntitiesEncoder implements Converter {
getDisplayname():string {
return "Encode HTML entities";
}
getId():string {
return "encodehtmlentities";
}
convert(input:string):string {
return input
.replace(/\&/g, "&amp;")
.replace(/\</g, "&lt;")
.replace(/\>/g, "&gt;")
.replace(/\"/g, "&quot;");
}
}

View File

@ -6,6 +6,12 @@ import {URIEncoder} from "./converter/uriencoder";
import {URIDecoder} from "./converter/uridecoder";
import {URIComponentEncoder} from "./converter/uricomponentencoder";
import {URIComponentDecoder} from "./converter/uricomponentdecoder";
import {HTMLEntitiesEncoder} from "./converter/htmlentitiesencoder";
import {HTMLEntitiesDecoder} from "./converter/htmlentitiesdecoder";
import {DecToHexConverter} from "./converter/dectohexconverter";
import {HexToDecConverter} from "./converter/hextodecconverter";
import {DecToBinConverter} from "./converter/dectobinconverter";
import {BinToDecConverter} from "./converter/bintodecconverter";
@Injectable()
export class ConverterregistryService {
@ -35,6 +41,12 @@ export class ConverterregistryService {
this.registerConverter(new URIDecoder());
this.registerConverter(new URIComponentEncoder());
this.registerConverter(new URIComponentDecoder());
this.registerConverter(new HTMLEntitiesEncoder());
this.registerConverter(new HTMLEntitiesDecoder());
this.registerConverter(new DecToHexConverter());
this.registerConverter(new HexToDecConverter());
this.registerConverter(new DecToBinConverter());
this.registerConverter(new BinToDecConverter());
}
private registerConverter(converter:Converter):void {

View File

@ -10,27 +10,6 @@
};
}
},
{
"id": "decodehtmlentities",
"name": "Decode HTML entities",
"convert": function (input) {
try {
return {
"status": "OK",
"content": input
.replace(/\&quot\;/g, "\"")
.replace(/\&gt\;/g, ">")
.replace(/\&lt\;/g, "<")
.replace(/\&amp\;/g, "&")
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid HTML entity string."
};
}
}
},
{
"id": "decodequotedprintable",
"name": "Decode quoted printable",
@ -48,54 +27,6 @@
}
}
},
{
"id": "hextodec",
"name": "Decode hex as decimal",
"convert": function (input) {
try {
return {
"status": "OK",
"content": parseInt(input, 16).toString(10)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid number (integer) string."
};
}
}
},
{
"id": "bintodec",
"name": "Decode binary as decimal",
"convert": function (input) {
try {
return {
"status": "OK",
"content": parseInt(input, 2).toString(10)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid number (integer) string."
};
}
}
},
{
"id": "encodehtmlentities",
"name": "Encode HTML entities",
"convert": function (input) {
return {
"status": "OK",
"content": input
.replace(/\&/g, "&amp;")
.replace(/\</g, "&lt;")
.replace(/\>/g, "&gt;")
.replace(/\"/g, "&quot;")
};
}
},
{
"id": "encodequotedprintable",
"name": "Encode quoted printable",
@ -105,40 +36,6 @@
"content": quotedPrintable.encode(utf8.encode(input))
};
}
},
{
"id": "dectohex",
"name": "Encode decimal as hex",
"convert": function (input) {
try {
return {
"status": "OK",
"content": parseInt(input).toString(16)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid number (integer) string."
};
}
}
},
{
"id": "dectobin",
"name": "Encode decimal as binary",
"convert": function (input) {
try {
return {
"status": "OK",
"content": parseInt(input).toString(2)
};
} catch (exception) {
return {
"status": "ERROR",
"content": "Invalid number (integer) string."
};
}
}
}
];