Add IpAddress.toBinaryString() method

This commit is contained in:
Borys_Levytskyi
2021-01-16 11:42:26 +02:00
parent 9d8b8a8665
commit 4cf12c2d89
2 changed files with 13 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
export default {
const formatter = {
formatString: function(num: number, kind: string) : string {
return num.toString(getBase(kind || "bin"));
},
@@ -32,3 +32,7 @@ function getBase(kind:string) : number {
throw new Error("Unsupported kind: " + kind);
}
export default formatter;
const emBin = formatter.emBin;
export {emBin};

View File

@@ -1,3 +1,5 @@
import {emBin} from "../core/formatter";
export type OctetNumber = 1 | 2 | 3 | 4;
export type NetworkClass = 'a' | 'b' | 'c' | 'd' | 'e';
@@ -33,6 +35,11 @@ export class IpAddress {
return `${this.firstByte}.${this.secondByte}.${this.thirdByte}.${this.fourthByte}`;
}
toBinaryString() {
return `${emBin(this.firstByte)}).${emBin(this.secondByte)}.${emBin(this.thirdByte)}.${emBin(this.fourthByte)}`;
}
clone(): IpAddress {
return new IpAddress(this.firstByte, this.secondByte, this.thirdByte, this.fourthByte);
}