An open API service indexing awesome lists of open source software.

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

NPM Package IP calculator based on (Netmask/CIDR/Host) with regex validation (Javascript).
https://github.com/snowfluke/ip-calc-regex

ipaddress ipcalc ipv4 mini-project network npm regex

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# ip-calc-regex

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

## Installation

```bash
npm install ip-calc-regex --save
```

## Usage

```js
// 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

```js
{
'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:

```js
{
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)

```js
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)

```js
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)

```js
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](https://github.com/snowfluke/ip-calc-regex)