From 01df7ac29c8af23cf1e74650c14294074d46e0ec Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Tue, 20 Sep 2016 21:12:13 +0200 Subject: [PATCH] added 6 more converters: - dec <-> bin - dec <-> hex - htmlentities de- and encode --- app/converter/bintodecconverter.ts | 14 ++++ app/converter/dectobinconverter.ts | 14 ++++ app/converter/dectohexconverter.ts | 14 ++++ app/converter/hextodecconverter.ts | 14 ++++ app/converter/htmlentitiesdecoder.ts | 19 +++++ app/converter/htmlentitiesencoder.ts | 19 +++++ app/converterregistry.service.ts | 12 ++++ dencode.js | 103 --------------------------- 8 files changed, 106 insertions(+), 103 deletions(-) create mode 100644 app/converter/bintodecconverter.ts create mode 100644 app/converter/dectobinconverter.ts create mode 100644 app/converter/dectohexconverter.ts create mode 100644 app/converter/hextodecconverter.ts create mode 100644 app/converter/htmlentitiesdecoder.ts create mode 100644 app/converter/htmlentitiesencoder.ts diff --git a/app/converter/bintodecconverter.ts b/app/converter/bintodecconverter.ts new file mode 100644 index 0000000..2c73959 --- /dev/null +++ b/app/converter/bintodecconverter.ts @@ -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); + } +} diff --git a/app/converter/dectobinconverter.ts b/app/converter/dectobinconverter.ts new file mode 100644 index 0000000..8cc2b0d --- /dev/null +++ b/app/converter/dectobinconverter.ts @@ -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); + } +} diff --git a/app/converter/dectohexconverter.ts b/app/converter/dectohexconverter.ts new file mode 100644 index 0000000..c0be47f --- /dev/null +++ b/app/converter/dectohexconverter.ts @@ -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); + } +} diff --git a/app/converter/hextodecconverter.ts b/app/converter/hextodecconverter.ts new file mode 100644 index 0000000..c6b3dba --- /dev/null +++ b/app/converter/hextodecconverter.ts @@ -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); + } +} diff --git a/app/converter/htmlentitiesdecoder.ts b/app/converter/htmlentitiesdecoder.ts new file mode 100644 index 0000000..2a793ce --- /dev/null +++ b/app/converter/htmlentitiesdecoder.ts @@ -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(/\"\;/g, "\"") + .replace(/\>\;/g, ">") + .replace(/\<\;/g, "<") + .replace(/\&\;/g, "&"); + } +} diff --git a/app/converter/htmlentitiesencoder.ts b/app/converter/htmlentitiesencoder.ts new file mode 100644 index 0000000..cf626b6 --- /dev/null +++ b/app/converter/htmlentitiesencoder.ts @@ -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, "&") + .replace(/\/g, ">") + .replace(/\"/g, """); + } +} diff --git a/app/converterregistry.service.ts b/app/converterregistry.service.ts index 7b89416..d1212b3 100644 --- a/app/converterregistry.service.ts +++ b/app/converterregistry.service.ts @@ -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 { diff --git a/dencode.js b/dencode.js index 7c73642..73607b3 100644 --- a/dencode.js +++ b/dencode.js @@ -10,27 +10,6 @@ }; } }, - { - "id": "decodehtmlentities", - "name": "Decode HTML entities", - "convert": function (input) { - try { - return { - "status": "OK", - "content": input - .replace(/\"\;/g, "\"") - .replace(/\>\;/g, ">") - .replace(/\<\;/g, "<") - .replace(/\&\;/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, "&") - .replace(/\/g, ">") - .replace(/\"/g, """) - }; - } - }, { "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." - }; - } - } } ];