# is-in-subnet

> Check if an IPv4 or IPv6 address is contained in the given CIDR subnet

Latest version **4.0.1** (published 2020-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-in-subnet
pnpm add is-in-subnet
yarn add is-in-subnet
bun add is-in-subnet
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2020-12-10 |
| First published | 2018-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.23.0 |
| Dependencies | 0 |
| Unpacked size | 150 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Author | Nate Silva |
| Maintainers | natesilva |
| Keywords | ip, address, ipv4, ipv6, cidr, networking |

## Links

- npm: https://www.npmjs.com/package/is-in-subnet
- Repository: https://github.com/natesilva/is-in-subnet
- Homepage: https://github.com/natesilva/is-in-subnet#readme
- Issues: https://github.com/natesilva/is-in-subnet/issues
- npm.io page: https://npm.io/package/is-in-subnet

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 4.0.1 (latest) — 2020-12-10
- 1.9.0 (next) — 2019-02-07
- 4.0.0 — 2020-12-10
- 3.2.0 — 2020-12-10
- 3.1.0 — 2020-01-22
- 3.0.1 — 2020-01-15
- 3.0.0 — 2020-01-15
- 2.1.0 — 2019-09-15
- 2.0.1 — 2019-09-15
- 2.0.0 — 2019-09-15
- 1.14.0 — 2019-09-15
- 1.13.0 — 2019-09-15
- 1.12.0 — 2019-09-15
- 1.11.0 — 2019-09-15
- 1.8.0 — 2019-02-05
- … 11 more at https://npm.io/package/is-in-subnet/versions

## README

# is-in-subnet [![npm](https://img.shields.io/npm/v/is-in-subnet.svg)](https://www.npmjs.com/package/is-in-subnet) [![dependencies](https://img.shields.io/david/natesilva/is-in-subnet.svg)](https://www.npmjs.com/package/is-in-subnet) [![license](https://img.shields.io/github/license/natesilva/is-in-subnet.svg)](https://github.com/natesilva/is-in-subnet/blob/master/LICENSE) [![node](https://img.shields.io/node/v/is-in-subnet.svg)](https://www.npmjs.com/package/p-ratelimit)

Check if an IPv4 or IPv6 address is contained in the given [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) subnet.

- Small
- Fast
- Simple syntax
- Full test coverage
- TypeScript-friendly
- Zero dependencies
- 🆕 [Browser-friendly](#in-a-browser)

See the 📖 [Reference](doc/reference.md) page for complete documentation.

## Installation

### With Node.js

`npm install is-in-subnet`

### In a browser

This module is compatible with Browserify and other bundling tools. If you prefer to load it as a single file from a CDN:

- <https://cdn.jsdelivr.net/npm/is-in-subnet@4/browser/isInSubnet.js>
- <https://cdn.jsdelivr.net/npm/is-in-subnet@4/browser/isInSubnet.min.js>

The module is loaded as a global named `isInSubnet`.

## Basic Usage

### Node.js

```javascript
const { isInSubnet } = require('is-in-subnet');

console.log(isInSubnet('10.5.0.1', '10.4.5.0/16')); // false
console.log(isInSubnet('10.5.0.1', '10.4.5.0/15')); // true

console.log(isInSubnet('2001:db8:f53a::1', '2001:db8:f53b::1:1/48')); // false
console.log(isInSubnet('2001:db8:f53a::1', '2001:db8:f531::1:1/44')); // true
```

### Browser

```javascript
console.log(isInSubnet.isInSubnet('10.5.0.1', '10.4.5.0/16'));
// if you don’t like that syntax, you may use this instead:
console.log(isInSubnet.check('10.5.0.1', '10.4.5.0/16'));
```

## More ways to use it

### Test multiple subnets at once

- You can pass an array instead of a single subnet.

```javascript
const inAnySubnet = isInSubnet('10.5.0.1', ['10.4.5.0/16', '192.168.1.0/24']);
```

### Amortise the parsing cost using a functional version

- `createChecker(subnetOrSubnets)` returns a function used to check an address

```javascript
const checker = createChecker(['10.4.5.0/16', '192.168.1.0/24']);
console.log(checker('10.5.0.1')); // true
```

### Test for special types of addresses

- `isPrivate(address)` — Private addresses (like `192.168.0.0`)
- `isLocalhost(address)` — Localhost addresses (like `::1`)

And more. See the [documentation](doc/reference.md#testing-for-special-address-ranges).

### Check if an address is valid

- `isIPv4(address)`, `isIPv6(address)`, `isIP(address)` — works just like the similarly-named functions in Node’s `net` module.

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