4.0.1 • Published 4 years ago
is-in-subnet v4.0.1
is-in-subnet
Check if an IPv4 or IPv6 address is contained in the given CIDR subnet.
- Small
- Fast
- Simple syntax
- Full test coverage
- TypeScript-friendly
- Zero dependencies
- 🆕 Browser-friendly
See the 📖 Reference 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
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
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.
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
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 (like192.168.0.0
)isLocalhost(address)
— Localhost addresses (like::1
)
And more. See the documentation.
Check if an address is valid
isIPv4(address)
,isIPv6(address)
,isIP(address)
— works just like the similarly-named functions in Node’snet
module.
3.2.0
4 years ago
4.0.1
4 years ago
4.0.0
4 years ago
3.1.0
5 years ago
3.0.1
5 years ago
3.0.0
5 years ago
2.1.0
5 years ago
2.0.1
5 years ago
2.0.0
5 years ago
1.14.0
5 years ago
1.13.0
5 years ago
1.12.0
5 years ago
1.11.0
5 years ago
1.9.0
6 years ago
1.8.0
6 years ago
1.7.0
6 years ago
1.6.0
6 years ago
1.5.0
7 years ago
1.4.1
7 years ago
1.4.0
7 years ago
1.3.0
7 years ago
1.2.0
7 years ago
1.1.1
7 years ago
1.1.0
7 years ago
1.0.1
7 years ago
1.0.0
7 years ago