Protected
Readonly
bitProtected
Readonly
firstProtected
Readonly
isProtected
Readonly
lastReturns the IP version as a number.
The IP version: 4
for IPv4 or 6
for IPv6.
Returns the stringified form of this IP or CIDR block in an abbreviated format.
This is a shorthand method of stringify with the format
option set to 'short'
.
Optional
capitalize: boolean = falseWhether to capitalize the output.
A properly formatted string representation of the IP or CIDR.
Checks whether the CIDR range associated with this instance contains the specified IP address.
The target IP address to check.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
true
if the CIDR range contains ipStr
, false
if not, or null
if ipStr
is invalid.
Checks whether the CIDR range associated with this instance contains all of the IP addresses in the array.
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
ipArr
is not an array or an empty array.
Checks whether the CIDR range associated with this instance contains any of the IP addresses in the array.
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
, or -1
if none match.
Checks whether the IP address associated with this intance is equal to a given IP address.
The IP address to compare.
true
if the IPs are equal, false
if not, or null
if ipStr
is invalid.
Checks whether the IP address associated with this intance is equal to all addresses in a given array.
An array of IP or CIDR strings or IP instances to compare against.
true
if all addresses are equal to this IP instance, false
otherwise, or
null
if ipArr
is not an array or an empty array.
Checks whether the IP address associated with this intance is equal to any address in a given array.
An array of IP or CIDR strings or IP instances to check against.
The index of the first match in ipArr
, or -1
if none match.
Gets the bit length of the current instance.
This always returns a number between 0-32
for IPv4 and 0-128
for IPv6.
To check whether the current instance represents a CIDR address, use IP.isCIDR.
The bit length as a number.
Gets a copy of the internal CIDR-related properties.
An object containing first
, last
, bitLen
, and isCidr
.
Gets range information of the IP instance.
Optional
getInstance: falseWhether to get the start and end IP addresses as IP instances.
Optional
options: StringifyOptionsOptional formatting options for the cidr
, first
,
and last
properties.
Gets range information of the IP instance.
Whether to get the start and end IP addresses as IP instances.
Optional
options: StringifyOptionsOptional formatting options for the cidr
property.
Returns the narrowest CIDR range that encompasses this IP and another IP address or subnet, provided they are of the same version (both IPv4 or both IPv6).
IP address or CIDR string to intersect with this instance.
Optional
options: IntersectOptions = {}Optional prefix length constraints and verbosity flag.
A new IP instance representing the narrowest common CIDR range, or null
if:
ip
is invalid.Checks whether the IP address associated with this instance is within all CIDR ranges in the array.
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 cidrArr
is
not an array or an empty array.
Checks whether the IP address associated with this instance is within any of the CIDR ranges in the array.
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, or -1
if none match.
Checks whether the IP address associated with this instance is within the CIDR range of another.
The CIDR string or IP instance representing the range.
Optional
options: CompareOptions = {}Options for comparing the input IPs.
A boolean indicating whether the IP address is within the CIDR range, or
null
if cidrStr
is invalid.
Returns the stringified form of this IP or CIDR block in a lengthened format.
This is a shorthand method of stringify with the format
option set to 'long'
.
Optional
capitalize: boolean = falseWhether to capitalize the output.
A properly formatted string representation of the IP or CIDR.
Returns the stringified form of this IP or CIDR block in a sanitized format.
This is a shorthand method of stringify with the format
option set to 'default'
.
Optional
capitalize: boolean = falseWhether to capitalize the output.
A properly formatted string representation of the IP or CIDR.
Returns the stringified form of this IP or CIDR block.
Optional
options: StringifyOptions = {}Optional formatting options. If omitted, a default "sanitized" format will be used.
A properly formatted string representation of the IP or CIDR.
Note: If the instance was initialized from an imprecise CIDR string, the output will reflect the corrected internal format.
const ip = IP.newFromText('fd12:3456:789a:1::1/64');
ip.stringify(); // fd12:3456:789a:1:0:0:0:0/64
Alias for IP.stringify with default options.
A stringified representation of the IP.
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 options for _getCommonRange.
The options to validate.
The validated options.
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
newInitializes an IP instance from a string and a range (i.e., a prefix length).
An IP address or CIDR-formatted string. If a CIDR string is passed,
the prefix length (e.g., /24
) is overridden by the range
parameter.
If an IP
instance is passed, the method attempts to create a new instance with the specified range
,
unless prevented by options
.
The desired CIDR prefix length (0–32
for IPv4, 0–128
for IPv6).
If ipStr
contains a CIDR prefix length smaller than range
(e.g., ipStr = "x.x.x.x/24"
, range = 25
),
the result may be non-obvious:
ipStr
is a string:const ip = IP.newFromRange('192.168.0.128/24', 25); // The /24 prefix length is ignored
console.log(ip?.toString()); // 192.168.0.128/25 — recalculated from the address and new prefix
ipStr
is an IP instance:const ip = IP.newFromText('192.168.0.128/24'); // IP instance with base address 192.168.0.0/24
const ip2 = IP.newFromRange(ip, 25);
console.log(ip2?.toString()); // 192.168.0.0/25 — based on the instance's base address and new prefix
Optional
options: ParseOptions = {}Optional parsing options for the input.
A new IP
instance if parsing succeeds; otherwise, null
if the input or range is invalid.
Static
newInitializes an IP instance from a string.
An IP- or CIDR-representing string.
If an IP
instance is passed, it will be cloned, unless prevented by options
.
Optional
options: ParseOptions = {}Optional parsing options for the input string.
A new IP
instance if parsing succeeds, or null
if the input is invalid.
The IP class. Unlike the static IPUtil class, this class provides several instance methods that can be used to perform validations on the same IP or CIDR address multiple times.
To initialize a new instance, use IP.newFromText or IP.newFromRange: