ip-subnet
Version:
A library of IP Subnetting utility functions
33 lines (32 loc) • 885 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subnet = void 0;
const cidr_1 = require("./cidr");
class Subnet {
constructor(networkAddress, netMask) {
this._netMask = netMask;
this._networkAddress = networkAddress.getNetworkAddress(netMask);
}
prefixLength() {
return (this._netMask.toBinaryString().match(/1/g) || []).length;
}
size() {
return Math.pow(2, 32 - this.prefixLength());
}
get networkAddress() {
return this._networkAddress;
}
get netMask() {
return this._netMask;
}
static parseCIDR(cidr) {
try {
const c = cidr_1.CIDR.parse(cidr);
return new Subnet(c.networkAddress, c.netMask);
}
catch (_a) {
throw new Error(`Invalid CIDR: ${cidr}`);
}
}
}
exports.Subnet = Subnet;