npm package: @inframaniac/ip-manager
Description: IP address utilities
Latest version: 1.0.7

# ip-manager

Simple utility library for validating CIDR addresses and working with IP ranges.

## Install

```sh
npm install ip-manager
```

## Basic usage

```ts
import ip from 'ip-manager';

const address = '50.165.190.0/23';

if (ip.validateCidr(address)) {
  console.log('Valid');
}
```

## API Examples

### `validateCidr(cidr: string)`

Check if the input is a valid IPv4 or IPv6 CIDR string.

```ts
ip.validateCidr('192.168.1.0/24'); // true
ip.validateCidr('invalid');        // false
```

### `firstIp(cidr: string)`

Return the first IP address of the CIDR block as a string.

```ts
ip.firstIp('192.168.1.0/30'); // '192.168.1.0'
```

### `lastIp(cidr: string)`

Return the last IP address of the block.

```ts
ip.lastIp('192.168.1.0/30'); // '192.168.1.3'
```

### `allIps(cidr: string)`

Return all addresses within the block as strings.

```ts
ip.allIps('192.168.1.0/30');
// ['192.168.1.0', '192.168.1.1', '192.168.1.2', '192.168.1.3']
```

### `ipsByLimit(cidr: string, from: number, limit: number)`

Get a set of addresses starting from an offset using a limit.

```ts
ip.ipsByLimit('192.168.1.0/30', 1, 2);
// ['192.168.1.1', '192.168.1.2']
```

### `ipsByRange(cidr: string, from: number, to: number)`

Get a set of addresses from one offset up to another offset (inclusive).

```ts
ip.ipsByRange('192.168.1.0/30', 1, 3);
// ['192.168.1.1', '192.168.1.2', '192.168.1.3']
```

### `freeIps(cidr: string, used: string[])`

Return the addresses within the CIDR block that are not included in the `used` array.

```ts
const used = ['192.168.1.0', '192.168.1.2'];
ip.freeIps('192.168.1.0/30', used);
// ['192.168.1.1', '192.168.1.3']
```

### IP Type & Version Utilities

```ts
ip.isIPv4('192.168.1.1'); // true
ip.isIPv6('fe80::1');     // true
ip.expandIPv6('fe80::1');   // 'fe80:0000:0000:0000:0000:0000:0000:0001'
ip.compressIPv6('fe80:0000:0000:0000:0000:0000:0000:0001'); // 'fe80::1'
```

### CIDR Supernet & Subnet Operations

```ts
ip.splitCidr('192.168.1.0/24', 26);
// ['192.168.1.0/26', '192.168.1.64/26', '192.168.1.128/26', '192.168.1.192/26']
ip.mergeCidrs(['192.168.1.0/25', '192.168.1.128/25']);
// ['192.168.1.0/24']
```

### IP Range Utilities

```ts
ip.rangeToCidr('192.168.1.0', '192.168.1.255');
ip.cidrToRange('192.168.1.0/24');
// { start: '192.168.1.0', end: '192.168.1.255' }
```

### Address Calculations

```ts
ip.nextIp('192.168.1.1'); // '192.168.1.2'
ip.prevIp('192.168.1.1'); // '192.168.1.0'
ip.ipDistance('192.168.1.1', '192.168.1.5'); // 4
```

### Reverse DNS / PTR Record Helper

```ts
ip.reverseDns('192.168.1.1'); // '1.1.168.192.in-addr.arpa'
```

### Network Classification

```ts
ip.getClass('10.0.0.1'); // 'A'
ip.isMulticast('224.0.0.1'); // true
ip.isLoopback('127.0.0.1'); // true
ip.isLinkLocal('169.254.1.1'); // true
```

### Random IPs

```ts
ip.randomIp('192.168.1.0/24');
// '192.168.1.xxx'
```

## Building and testing

To compile the TypeScript sources and run the test suite:

```sh
npm run build
npm test
```