Function for subnet mask
I use the following functions for netmask to CIDR and CIDR to netmask conversions:
var netmask2CIDR = (netmask) => (netmask.split('.').map(Number)
.map(part => (part >>> 0).toString(2))
.join('')).split('1').length -1;
var CIDR2netmask = (bitCount) => {
var mask=[];
for(var i=0;i<4;i++) {
var n = Math.min(bitCount, 8);
mask.push(256 - Math.pow(2, 8-n));
bitCount -= n;
}
return mask.join('.');
}
Hope this helps!
Just finished writing it, and then realized that it would look cleaner with switch case
:
For anyone's reference. If there is in fact some JS function or API that will make this a lot cleaner and simpler, please answer and I'll mark it.
switch(ret_mask = path.mask) {
case "0.0.0.0":
return ret_mask = "/0";
case "128.0.0.0":
return ret_mask = "/1";
case "192.0.0.0":
return ret_mask = "/2";
case "224.0.0.0":
return ret_mask = "/3";
case "240.0.0.0":
return ret_mask = "/4";
case "248.0.0.0":
return ret_mask = "/5";
case "252.0.0.0":
return ret_mask = "/6";
case "254.0.0.0":
return ret_mask = "/7";
case "255.0.0.0":
return ret_mask = "/8";
case "255.128.0.0":
return ret_mask = "/9";
case "255.192.0.0":
return ret_mask = "/10";
case "255.224.0.0":
return ret_mask = "/11";
case "255.240.0.0":
return ret_mask = "/12";
case "255.248.0.0":
return ret_mask = "/13";
case "255.252.0.0":
return ret_mask = "/14";
case "255.254.0.0":
return ret_mask = "/15";
case "255.255.0.0":
return ret_mask = "/16";
case "255.255.128.0":
return ret_mask = "/17";
case "255.255.192.0":
return ret_mask = "/18";
case "255.255.224.0":
return ret_mask = "/19";
case "255.255.240.0":
return ret_mask = "/20";
case "255.255.248.0":
return ret_mask = "/21";
case "255.255.252.0":
return ret_mask = "/22";
case "255.255.254.0":
return ret_mask = "/23";
case "255.255.255.0":
return ret_mask = "/24";
case "255.255.255.128":
return ret_mask = "/25";
case "255.255.255.192":
return ret_mask = "/26";
case "255.255.255.224":
return ret_mask = "/27";
case "255.255.255.240":
return ret_mask = "/28";
case "255.255.255.248":
return ret_mask = "/29";
case "255.255.255.252":
return ret_mask = "/30";
case "255.255.255.254":
return ret_mask = "/31";
case "255.255.255.255":
return ret_mask = ""
}