Last active
April 10, 2023 16:46
-
-
Save imtrinity94/5648dc38ce45e4149dcc451dd4274d8f to your computer and use it in GitHub Desktop.
vRO_convertToCIDRNotation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @module com.mayank.goyal | |
* @version 4.0.0 | |
* | |
* @param {string} gatewayIP 192.10.21.0 | |
* @param {string} subnetmaskIP 255.255.255.192 | |
* | |
* @outputType string 192.10.21.0/26 | |
* | |
*/ | |
function convertToCIDRNotation(gatewayIP,subnetMaskIP){ | |
this.start = gatewayIP; | |
this.subnetMask = subnetMaskIP; | |
this.cidrMask = function(){ | |
mask = this.subnetMask.split("."); | |
if(mask.length != 4){ | |
throw("Precondition failed: subnet mask should only have four octets!"); | |
} | |
bits = 0; | |
for(var i = 0; i < mask.length; i += 1){ | |
octet = parseInt(mask[i]); | |
if(octet > 255 || octet < 0){ | |
throw("Precondition failed: octet out of range!"); | |
} | |
while(octet > 0){ | |
bits += octet % 2; | |
octet = octet >> 1; | |
} | |
} | |
if(bits > 32){ | |
throw("Postcondition failed: Too many bits!"); | |
} | |
return bits; | |
}.bind(this); | |
}; | |
cidrIp = new convertToCIDRNotation("192.168.1.0","255.255.255.192"); | |
return (cidrIp.start + "/" + cidrIp.cidrMask()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment