# ip-cidr

> Module for working with CIDR (v4, v6)

Latest version **4.0.2** (published 2024-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ip-cidr
pnpm add ip-cidr
yarn add ip-cidr
bun add ip-cidr
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2024-07-28 |
| First published | 2016-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.14.0 |
| Dependencies | 1 |
| Unpacked size | 130.4 KB |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Alexander Balasyan |
| Maintainers | ortex |
| Keywords | ip, cidr, range, from, to, array |

## Links

- npm: https://www.npmjs.com/package/ip-cidr
- Repository: https://github.com/ortexx/ip-cidr
- Homepage: https://github.com/ortexx/ip-cidr#readme
- Issues: https://github.com/ortexx/ip-cidr/issues
- npm.io page: https://npm.io/package/ip-cidr

## Dependencies (1)

- [ip-address](https://npm.io/package/ip-address.md) ^9.0.5

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 4.0.2 (latest) — 2024-07-28
- 4.0.1 — 2024-06-28
- 4.0.0 — 2023-12-17
- 3.1.0 — 2023-01-30
- 3.0.11 — 2022-11-15
- 3.0.10 — 2022-05-27
- 3.0.9 — 2022-05-26
- 3.0.8 — 2022-05-25
- 3.0.7 — 2022-04-18
- 3.0.4 — 2021-08-10
- 3.0.3 — 2021-06-21
- 3.0.2 — 2021-06-14
- 3.0.1 — 2021-06-11
- 3.0.0 — 2021-06-04
- 2.1.5 — 2021-06-04
- … 35 more at https://npm.io/package/ip-cidr/versions

## README

# Install  
`npm install ip-cidr`

# About  
Module for working with CIDR (v4, v6). Based on [ip-address](https://github.com/beaugunderson/ip-address).
Since v4+ using javascript BigInt for big numbers handling.

# Example  

```js
import IPCIDR from 'ip-cidr';
const address = "50.165.190.0/23";

if(!IPCIDR.isValidCIDR(address)) {
  return;
}

const cidr = new IPCIDR(address); 

// get start ip address as a string
cidr.start(); 

// get end ip address as a big integer
cidr.end({ type: "bigInteger" }); 

// do something with each element of the range  
cidr.loop(ip => console.log(ip), { type: "addressObject" });

// get an array of all ip addresses in the range as a big integer;
cidr.toArray({ type: "bigInteger" }); 

// get an array by chunks using from/limit
cidr.toArray({ from: 1, limit: 2n });

// get an array by chunks using from/to
cidr.toArray({ from: BigInt('1'), to: 3 });
cidr.toArray({ from: '50.165.190.1', to: '50.165.190.3' });

// get an array of start and end ip addresses as a string [startIpAsString, endIpAsString]
cidr.toRange(); 
```

## Client side
Load __/dist/ip-cidr.js__ as a script and you can get the library in __window.IPCIDR__

# API  
### .formatIP(address, [options])  
to return an "ip-address" module object in the necessary format 

### .isValidAddress(address)  
to check the address is valid or not (ip or cidr)

### .isValidCIDR(address)  
to check the address is valid (only cidr)

### .createAddress(address)  
to create an object address from the string

### .prototype.contains(address)  
to check the address belongs to the range

### .prototype.start([options])  
to get the start ip address

### .prototype.end([options])  
to get the end ip address

### .prototype.toString()   
to convert the cidr to a string like "50.165.190.0/23"

### .prototype.toRange([options])  
to convert the cidr to an array with start and end ip addresses [startIp, endIp]

### .prototype.toObject([options])   
to convert the cidr to an object with start and end ip addresses {start: startIp, end: endIp}

### .prototype.toArray([options], [results])  
to convert the cidr to an array with all ip addresses in the range  
you can get information by chunks using **options.from/options.limit** or **options.from/options.to**  
you can pass the second argument "results" (object) to get all chunk pagination information

### .prototype.loop(fn, [options], [results])  
to run __fn__ for each element of the range  
you can use the same chunk options as in __.toArray()__

---
_Source: https://npm.io/package/ip-cidr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
