IP Calculator based on Netmask/CIDR/Host with regex validation.
npm install ip-calc-regex --save// ES6 import
import { ipCalc } from 'ip-calc-regex';
// CommonJS require
const { ipCalc } = require('ip-calc-regex');
// Example: Calculate using CIDR notation
const result = ipCalc('192.168.1.1', 1, '24');
console.log(result);{
'IP Address': '192.168.1.1',
'Netmask': '255.255.255.0',
'CIDR': '/24',
'Wildcard': '0.0.0.255',
'Class': 'Class C',
'Network Address': '192.168.1.0',
'Host min': '192.168.1.1',
'Host max': '192.168.1.254',
'Broadcast': '192.168.1.255',
'Total Subnets': 256,
'Total Hosts': '254'
}If invalid input is provided, the function returns an error object:
{
errorMsg: 'Invalid IP Address!' // or other error message
}| Parameter | Type | Description | Values |
|---|---|---|---|
ipAddress |
string | IPv4 address | e.g., '192.168.100.1' |
type |
number | Input type | 0 = Netmask1 = CIDR2 = Total Hosts |
typeValue |
string | Value corresponding to the type | Depends on type parameter |
Returns an object containing:
IP Address: Original IP addressNetmask: Subnet mask in dotted decimal notationCIDR: CIDR notation (e.g., /24)Wildcard: Wildcard maskClass: IP address class (A, B, C, D, or E)Network Address: Network addressHost min: First usable host addressHost max: Last usable host addressBroadcast: Broadcast addressTotal Subnets: Total number of subnetsTotal Hosts: Total usable hosts (formatted with dot separators)
Or an error object with errorMsg property if validation fails.
ipCalc('192.168.1.1', 0, '255.255.255.0')
// Output:
// {
// 'IP Address': '192.168.1.1',
// 'Netmask': '255.255.255.0',
// 'CIDR': '/24',
// 'Wildcard': '0.0.0.255',
// 'Class': 'Class C',
// 'Network Address': '192.168.1.0',
// 'Host min': '192.168.1.1',
// 'Host max': '192.168.1.254',
// 'Broadcast': '192.168.1.255',
// 'Total Subnets': 256,
// 'Total Hosts': '254'
// }ipCalc('172.20.78.0', 1, '24')
// Output:
// {
// 'IP Address': '172.20.78.0',
// 'Netmask': '255.255.255.0',
// 'CIDR': '/24',
// 'Wildcard': '0.0.0.255',
// 'Class': 'Class B',
// 'Network Address': '172.20.78.0',
// 'Host min': '172.20.78.1',
// 'Host max': '172.20.78.254',
// 'Broadcast': '172.20.78.255',
// 'Total Subnets': 65536,
// 'Total Hosts': '254'
// }ipCalc('192.168.1.1', 2, '12')
// Output:
// {
// 'IP Address': '192.168.1.1',
// 'Netmask': '255.240.0.0',
// 'CIDR': '/28',
// 'Wildcard': '0.15.255.255',
// 'Class': 'Class C',
// 'Network Address': '192.160.0.0',
// 'Host min': '192.160.0.1',
// 'Host max': '192.175.255.254',
// 'Broadcast': '192.175.255.255',
// 'Total Subnets': 16,
// 'Total Hosts': '14'
// }The calculator validates:
- IP Address: Must be a valid IPv4 address (0-255 for each octet)
- Netmask: Must be a valid subnet mask
- CIDR: Must be between 1 and 32
- Host Count: Must be a positive integer
- ✅ Supports all IP classes (A, B, C, D, E)
- ✅ Works with any valid CIDR notation (1-32)
- ✅ Regex-based input validation
- ✅ Calculates network address, broadcast, and host range
- ✅ Computes total subnets and usable hosts
- ✅ Well-documented code with JSDoc comments
- ✅ No external dependencies
ISC
Awal Ariansyah