What is is-cidr?
The is-cidr npm package is a utility for validating and parsing CIDR (Classless Inter-Domain Routing) notations. It helps in determining whether a given string is a valid CIDR notation for IPv4 or IPv6 addresses.
What are is-cidr's main functionalities?
Validate IPv4 CIDR
This feature allows you to check if a given string is a valid IPv4 CIDR notation. The function returns true if the input is a valid IPv4 CIDR and false otherwise.
const isCidr = require('is-cidr');
console.log(isCidr.v4('192.168.0.1/24')); // true
console.log(isCidr.v4('192.168.0.1')); // false
Validate IPv6 CIDR
This feature allows you to check if a given string is a valid IPv6 CIDR notation. The function returns true if the input is a valid IPv6 CIDR and false otherwise.
const isCidr = require('is-cidr');
console.log(isCidr.v6('2001:db8::/32')); // true
console.log(isCidr.v6('2001:db8::')); // false
Validate both IPv4 and IPv6 CIDR
This feature allows you to check if a given string is a valid CIDR notation for either IPv4 or IPv6. The function returns 4 for valid IPv4 CIDR, 6 for valid IPv6 CIDR, and 0 for invalid CIDR.
const isCidr = require('is-cidr');
console.log(isCidr('192.168.0.1/24')); // 4
console.log(isCidr('2001:db8::/32')); // 6
console.log(isCidr('invalid-cidr')); // 0
Other packages similar to is-cidr
cidr-regex
The cidr-regex package provides regular expressions for matching CIDR notations. It can be used to validate and extract CIDR notations from strings. Unlike is-cidr, it focuses on pattern matching using regular expressions.
ip-cidr
The ip-cidr package offers more comprehensive CIDR manipulation capabilities, including validation, parsing, and conversion between CIDR and IP ranges. It provides a broader set of functionalities compared to is-cidr, which is primarily focused on validation.
netmask
The netmask package is used for IP address and network mask manipulation. It allows for CIDR validation, subnet calculations, and IP range generation. It offers more advanced network-related functionalities compared to the simpler validation focus of is-cidr.
is-cidr
Check if a string is an IP address in CIDR notation
Install
npm i is-cidr
Usage
import isCidr from "is-cidr";
isCidr("192.168.0.1/24");
isCidr("1:2:3:4:5:6:7:8/64");
isCidr("10.0.0.0");
isCidr.v6("10.0.0.0/24");
API
isCidr(input)
Check if input
is a IPv4 or IPv6 CIDR. Returns either 4
, 6
(indicating the IP version) or 0
if the string is not a CIDR.
isCidr.v4(input)
Check if input
is a IPv4 CIDR. Returns a boolean.
isCidr.v6(input)
Check if input
is a IPv6 CIDR. Returns a boolean.
Related
- ip-bigint - Convert IPv4 and IPv6 addresses to native BigInt and vice-versa
- ip-regex - Regular expression for matching IP addresses
- is-ip - Check if a string is an IP address
- cidr-regex - Check if a string is an IP address in CIDR notation
- cidr-tools - Tools to work with IPv4 and IPv6 CIDR network lists
License
© silverwind, distributed under BSD licence
Based on previous work by Felipe Apostol