Skip to content

snowfluke/ip-calc-regex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ip-calc-regex

IP Calculator based on Netmask/CIDR/Host with regex validation.

Installation

npm install ip-calc-regex --save

Usage

// 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);

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'
}

Error Handling

If invalid input is provided, the function returns an error object:

{
  errorMsg: 'Invalid IP Address!' // or other error message
}

API

ipCalc(ipAddress, type, typeValue)

Parameters

Parameter Type Description Values
ipAddress string IPv4 address e.g., '192.168.100.1'
type number Input type 0 = Netmask
1 = CIDR
2 = Total Hosts
typeValue string Value corresponding to the type Depends on type parameter

Return Value

Returns an object containing:

  • IP Address: Original IP address
  • Netmask: Subnet mask in dotted decimal notation
  • CIDR: CIDR notation (e.g., /24)
  • Wildcard: Wildcard mask
  • Class: IP address class (A, B, C, D, or E)
  • Network Address: Network address
  • Host min: First usable host address
  • Host max: Last usable host address
  • Broadcast: Broadcast address
  • Total Subnets: Total number of subnets
  • Total Hosts: Total usable hosts (formatted with dot separators)

Or an error object with errorMsg property if validation fails.

Examples

Using Netmask (type = 0)

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'
// }

Using CIDR Notation (type = 1)

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'
// }

Using Host Count (type = 2)

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'
// }

Validation

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

Features

  • ✅ 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

License

ISC

Author

Awal Ariansyah

Repository

https://github.com/snowfluke/ip-calc-regex

About

NPM Package IP calculator based on (Netmask/CIDR/Host) with regex validation (Javascript).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •