@cloudron/ipaddr
v1.1.0
Published
IP address and CIDR helpers
Readme
@cloudron/ipaddr
Small IP address and CIDR helpers used by Cloudron services.
Install
npm install @cloudron/ipaddrUsage
import ipaddr, { isValid, isValidCIDR, isEqual, includes, parseCIDR } from '@cloudron/ipaddr';
isValid('1.2.3.4'); // true
isValidCIDR('172.18.0.0/16'); // true
isEqual('::ffff:1.2.3.4', '1.2.3.4'); // true
includes('10.0.0.0/8', '10.1.2.3'); // true
parseCIDR('172.18.0.0/16'); // { ip: '172.18.0.0', prefix: 16, family: 4 }
// default export has the same helpers
ipaddr.isValid('::1'); // trueAPI
isValid(ip)
Returns true if ip is a valid IPv4 or IPv6 address, otherwise false.
parseCIDR(cidr)
Parses a CIDR string and returns { ip, prefix, family } (family is 4 or 6).
Returns null for invalid CIDR input.
isValidCIDR(cidr)
Returns true if cidr is a valid CIDR string (IPv4 or IPv6), otherwise false.
isEqual(ip1, ip2)
Compares two IP addresses and returns true if they represent the same address.
This normalizes different textual forms (for example IPv6 shorthand and IPv4-mapped IPv6).
includes(cidr, ip)
Returns true if ip is inside the provided cidr range, otherwise false.
Notes
- All functions expect string inputs.
- Invalid values return
falsefor validation and comparison checks. - Both named exports and default export are supported.
