ip-navigator
Version:
A tool for IP address manipulation and calculation
139 lines • 5.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidBinaryIP = exports.isValidCIDR = exports.isValidSubnetMask = exports.isValidIPAddress = void 0;
var validation_1 = require("../lib/validation");
var index_1 = require("../conversion/index");
/**
* Validates whether the given string is a valid IPv4 address.
*
* @param {string} ipAddress - The IP address to validate.
* @returns {boolean} True if the IP address is valid, false otherwise.
*
* @example
* isValidIPAddress('192.168.1.1'); // returns true
* isValidIPAddress('256.1.2.3'); // returns false
* isValidIPAddress('192.168.1'); // returns false
* isValidIPAddress('192.168.1.1.5'); // returns false
* isValidIPAddress(' 192.168.1.1'); // returns false
* isValidIPAddress('192.168.1.1 '); // returns false
*
*
*/
var isValidIPAddress = function (ipAddress) {
// Check for leading or trailing whitespace
if (ipAddress !== ipAddress.trim())
return false;
// Split the IP address into octets
var octets = ipAddress.split(".");
// Check if we have exactly 4 octets
if (octets.length !== 4)
return false;
// Validate each octet
for (var _i = 0, octets_1 = octets; _i < octets_1.length; _i++) {
var octet = octets_1[_i];
if (!(0, validation_1.isValidDecimalOctet)(octet))
return false;
}
// If we've passed all checks, the IP is valid
return true;
};
exports.isValidIPAddress = isValidIPAddress;
/**
* Validates whether the given string is a valid subnet mask.
*
* @param {string} subnetMask - The subnet mask to validate.
* @returns {boolean} True if the subnet mask is valid, false otherwise.
*
* @example
* isValidSubnetMask('255.255.255.0'); // returns true
* isValidSubnetMask('255.255.256.0'); // returns false
* isValidSubnetMask('255.255.254.0'); // returns true
* isValidSubnetMask('255.255.253.0'); // returns false
*
*
*/
var isValidSubnetMask = function (subnetMask) {
// Check if the subnet mask is a valid IP address
if (!(0, exports.isValidIPAddress)(subnetMask))
return false;
// Convert the subnet mask to binary
var binaryIP = (0, index_1.ipToBinary)(subnetMask);
// Find the first zero in the binary representation
var zeroIndex = binaryIP.indexOf("0");
// Check if there are no zeros in the subnet mask
var noZero = zeroIndex === -1;
// Check if there are no ones after the first zero
var noOneAfterZero = !binaryIP.slice(zeroIndex).includes("1");
// If both conditions are met, the subnet mask is valid
return noZero || noOneAfterZero;
};
exports.isValidSubnetMask = isValidSubnetMask;
/**
* Validates whether the given string is a valid CIDR notation.
*
* @param {string} cidr - The CIDR notation to validate.
* @returns {boolean} True if the CIDR notation is valid, false otherwise.
*
* @example
* isValidCIDR('192.168.1.0/24'); // returns true
* isValidCIDR('192.168.1.0/33'); // returns false
* isValidCIDR('192.168.1.0'); // returns false (missing prefix)
* isValidCIDR('192.168.1.1/24/32'); // returns false (multiple slashes)
* isValidCIDR('192.168.1.1/24.5'); // returns false (decimal prefix)
* isValidCIDR('192.168.1.1/'); // returns false (empty prefix)
*
*
*/
var isValidCIDR = function (cidr) {
// Check for leading or trailing whitespace
if (cidr.trim() !== cidr)
return false;
// Split the CIDR notation into IP address and prefix
var parts = cidr.split("/");
// Check if we have exactly 2 parts
if (parts.length !== 2)
return false;
// Destructure the parts into IP address and prefix
var ipAddress = parts[0], prefix = parts[1];
// Check if the IP address is valid
if (!(0, exports.isValidIPAddress)(ipAddress))
return false;
// Check if the prefix is a valid
if (!(0, validation_1.isValidPrefix)(prefix))
return false;
// If we've passed all checks, the CIDR notation is valid
return true;
};
exports.isValidCIDR = isValidCIDR;
/**
* Validates whether the given string is a valid binary IP address.
*
* @param {string} binaryIP - The binary IP address to validate.
* @returns {boolean} True if the binary IP address is valid, false otherwise.
*
* @example
* isValidBinaryIP('11000000.10101000.00000001.00000001'); // returns true
* isValidBinaryIP('11000000.10101000.00000001.0000000'); // returns false
* isValidBinaryIP('11000000.10101000.00000001.00000001.0'); // returns false
*
*
*/
var isValidBinaryIP = function (binaryIP) {
// Check if the binary string is a valid length
if (binaryIP.length !== 35)
return false;
// Split the binary string into octets
var octets = binaryIP.split(".");
// Check if we have exactly 4 octets
if (octets.length !== 4)
return false;
// Validate each octet
octets.forEach(function (octet) {
if (!(0, validation_1.isValidBinaryOctet)(octet))
return false;
});
// If we've passed all checks, the binary string is valid
return true;
};
exports.isValidBinaryIP = isValidBinaryIP;
//# sourceMappingURL=index.js.map