Protected
Static
_compareProtected
Compares two IP address ranges to determine whether they are equal or one contains the other.
Range object of the first IP.
IP string or IP instance to compare against.
Use <
to check if ip2
contains ip1
, >
if ip1
contains ip2
,
or =
if ip1
and ip2
are exactly equal.
Optional
options: CompareOptions = {}Options to control comparison behavior.
CompareOptions.excludeEquivalent has no effect for comparator === '='
.
null
if ip2
is not a valid IP; true
if comparison passes; false
otherwise.
Protected
Static
_getProtected
Computes the narrowest CIDR range that fully encompasses two IP ranges, while enforcing optional prefix length constraints.
Supports both IPv4 and IPv6, as long as both input ranges are of the same version.
The input ranges must be pre-parsed RangeObject objects (with .first
and .last
arrays).
Behavior:
null
.null
.verbose
is true
, logs warnings for invalid inputs, version mismatches,
and out-of-bound results to the console.Options:
The options
object can include the following optional prefix constraints and flags:
minV4
: Minimum allowed prefix length for IPv4 (default: 0
, i.e. /0
)maxV4
: Maximum allowed prefix length for IPv4 (default: 32
, i.e. /32
)minV6
: Minimum allowed prefix length for IPv6 (default: 0
, i.e. /0
)maxV6
: Maximum allowed prefix length for IPv6 (default: 128
, i.e. /128
)verbose
: If true
, warnings are logged to the console instead of silently returning null
Errors: Throws when:
minV4 > maxV4
, or maxV6 > 128
).First IP range (must have .first
and .last
arrays).
Second IP range (same version as range1
).
Optional
options: IntersectOptions = {}Optional prefix length bounds and debug flag.
A CIDR-aligned RangeObject
covering both inputs,
or null
if the IP versions differ or the result violates constraints.
Protected
Static
_getProtected
Converts an IP string or IP instance into a range object.
IP/CIDR string or IP instance.
Optional
options: ParseOptions & { Optional parsing options for ip
.
Range object for the given IP, or null
if invalid.
Protected
Static
_parseProtected
Parses a string potentially representing an IP address or CIDR.
Supports both IPv4 and IPv6 formats, optionally with CIDR bit lengths.
Returns null
if the input is invalid or outside expected ranges.
Accepted formats:
'x.x.x.x'
or 'x.x.x.x/bitLen'
, where x = 0–255
, bitLen = 0–32
'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx'
or shortened '::'
forms,
optionally with '/bitLen'
, where bitLen = 0–128
Limitations:
::ffff:192.168.0.1
)The string to parse.
Optional
options: { Optional parsing options.
If provided, overrides any CIDR bit length in ipStr
.
A parsed object with parts and optional bit length, or null
if invalid.
bitLen
will be null
if:
suppressFullLengthCidr
is true
, and the parsed or specified bit length is either 32
for IPv4 or 128
for IPv6.Protected
Static
_parseProtected
Parses and stringifies an IP string with optional filtering.
IP address or CIDR string to parse.
Formatting options for output.
Formatted IP string, or null
if:
conditionPredicate
.Protected
Static
_parseProtected
Returns the first and last IPs in the given CIDR range.
Accepts both IPv4 and IPv6 addresses, represented as arrays of decimal parts.
If no bitLen
is provided, the address is treated as a single host (non-CIDR).
Array of decimal IP parts:
0–255
)0–65535
)Optional CIDR bit length (0–32
for IPv4, 0–128
for IPv6).
Object with the first and last IPs in the range.
Protected
Static
_stringifyProtected
Converts an array of decimal IP parts into a string.
Array of decimal IP parts.
String to append after the address (e.g., CIDR /24
or empty string).
Optional
options: StringifyOptions = {}Formatting options.
Formatted IP address string.
Protected
Static
_validateProtected
Validates whether a string is a valid IP or CIDR address.
allowCidr
is true
, CIDR suffixes are allowed.allowCidr
is 'strict'
, the method returns a normalized CIDR string if the input is valid
but not in canonical form.allowCidr
is false
, CIDR suffixes are disallowed.true
: The input is valid.false
: The input is invalid.string
: A corrected CIDR string (only if allowCidr === 'strict'
and normalization is needed).The IP or CIDR string to validate.
Whether to allow CIDRs, or require strict CIDR format.
Optional
options: Omit<IPOptions, "suppressFullLengthCidr"> = {}Optional formatting and parsing options.
See above.
Protected
Static
_validateProtected
Validates options for _getCommonRange.
The options to validate.
The validated options.
Static
abbreviateReturns an abbreviated representation of an IP string.
Examples:
192.168.0.1
(IPv4; same as IPUtil.sanitize)fd12:3456:789a:1::
Inaccurate CIDRs are corrected automatically:
fd12:3456:789a:1:0:0:0:1/64
fd12:3456:789a:1::/64
Abbreviated string, or null
if:
conditionPredicate
.Static
cleanRemoves Unicode bidirectional control characters and trims whitespace.
These invisible characters (e.g., LRM, RLM, directional overrides) can appear when copying IPs from external sources, especially in web editors.
Their presence can interfere with IP parsing, matching, or display logic.
The specific characters removed are:
This logic mirrors cleanup done in MediaWiki core: TitleParser::splitTitleString
Input string (may contain invisible bidi characters).
Cleaned string, safe for IP parsing.
Static
containsChecks whether a CIDR range contains the specified IP address.
The CIDR string or IP instance representing the containing range.
The target IP address to check.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
true
if cidrStr
contains ipStr
, false
if not, or null
if either input is invalid.
Static
containsChecks whether a CIDR range contains all of the IP addresses in the array.
The CIDR string or IP instance representing the containing range.
An array of IP or CIDR strings or IP instances to test.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
true
if all IPs are contained, false
if any are not, or null
if cidrStr
is invalid or
ipArr
is not an array or an empty array.
Static
containsChecks whether a CIDR range contains any of the IP addresses in the array.
The CIDR string or IP instance representing the containing range.
An array of IP or CIDR strings or IP instances to test.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
The index of the first match in ipArr
, -1
if none match, or null
if cidrStr
is invalid.
Static
equalsStatic
equalsStatic
equalsStatic
intersectReturns the narrowest CIDR range that encompasses two IP addresses or subnets, provided they are of the same version (both IPv4 or both IPv6).
First IP address or CIDR to intersect.
Second IP address or CIDR to intersect.
Optional
options: IntersectOptions = {}Optional prefix length constraints and verbosity flag.
An IP instance representing the narrowest common range, or null
if:
ip1
or ip2
is invalid.Static
isCIDRChecks whether the input is a valid CIDR (either IPv4 or IPv6).
The CIDR string to check.
Optional
mode: "strict"Require strict CIDR formatting if 'strict'
is passed.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isIPChecks whether the input is a valid IP or CIDR address.
The IP or CIDR string to check.
Optional
allowCidr: boolean | "strict" = falseWhether to allow CIDRs, or require strict CIDR format.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isIPv4Checks whether the input is a valid IPv4 address or IPv4 CIDR.
The IPv4 or IPv4 CIDR string to check.
Optional
allowCidr: boolean | "strict" = falseWhether to allow CIDRs, or require strict CIDR format.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isIPv4CIDRChecks whether the input is a valid IPv4 CIDR.
The IPv4 CIDR string to check.
Optional
mode: "strict"Require strict CIDR formatting if 'strict'
is passed.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isIPv6Checks whether the input is a valid IPv6 address or IPv6 CIDR.
The IPv6 or IPv6 CIDR string to check.
Optional
allowCidr: boolean | "strict" = falseWhether to allow CIDRs, or require strict CIDR format.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isIPv6CIDRChecks whether the input is a valid IPv6 CIDR.
The IPv6 CIDR string to check.
Optional
mode: "strict"Require strict CIDR formatting if 'strict'
is passed.
Optional
options: StringifyOptions = {}Formatting options for corrected CIDRs.
Returns true
if valid, false
if invalid, or a normalized CIDR string.
Static
isChecks whether a given IP address is within all CIDR ranges in the array.
The IP address to evaluate.
An array of CIDR strings or IP instances to check against.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
true
if the IP is within all CIDRs, false
if not, or null
if ipStr
is invalid
or cidrArr
is not an array or an empty array.
Static
isChecks whether a given IP address is within any of the CIDR ranges in the array.
The IP address to evaluate.
An array of CIDR strings or IP instances to check against.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
The index of the first matching CIDR in the array, -1
if none match, or null
if ipStr
is invalid.
Static
isChecks whether a given IP address is within the CIDR range of another.
The target IP address to evaluate.
The CIDR string or IP instance representing the range.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
true
if ipStr
is within the range of cidrStr
, false
if not, or null
if either input is invalid.
Static
lengthenReturns a fully expanded (lengthened) representation of an IP string.
Examples:
192.168.000.001
fd12:3456:789a:0001:0000:0000:0000:0000
Inaccurate CIDRs are corrected automatically:
fd12:3456:789a:1:0:0:0:1/64
fd12:3456:789a:0001:0000:0000:0000:0000/64
Expanded string, or null
if:
conditionPredicate
.Static
sanitizeReturns a sanitized representation of an IP string.
Examples:
192.168.0.1
(IPv4; same as IPUtil.abbreviate)fd12:3456:789a:1:0:0:0:0
Inaccurate CIDRs are corrected automatically:
fd12:3456:789a:1::1/64
fd12:3456:789a:1:0:0:0:0/64
Sanitized string, or null
if:
conditionPredicate
.
A utility class that provides static methods for validating and formatting IP and CIDR strings. Unlike the IP class, these methods are stateless and ideal for one-off checks or transformations on varying inputs.