Class IPUtil

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.

Hierarchy

  • IPBase
    • IPUtil

Methods

  • Protected

    Compares two IP address ranges to determine whether they are equal or one contains the other.

    Parameters

    • ip1: RangeObject

      Range object of the first IP.

    • ip2: string | IP

      IP string or IP instance to compare against.

    • comparator: "<" | ">" | "="

      Use < to check if ip2 contains ip1, > if ip1 contains ip2, or = if ip1 and ip2 are exactly equal.

    • Optionaloptions: CompareOptions = {}

      Options to control comparison behavior.

      CompareOptions.excludeEquivalent has no effect for comparator === '='.

    Returns null | boolean

    null if ip2 is not a valid IP; true if comparison passes; false otherwise.

  • Protected

    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:

    • Returns the smallest CIDR block that includes both input ranges.
    • If the IP versions differ (e.g. one is IPv4 and the other is IPv6), returns null.
    • If the computed prefix length falls outside the allowed bounds, returns null.
    • If 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:

    • Any of the prefix constraint values are not numbers.
    • Any of the values are out of bounds (e.g. minV4 > maxV4, or maxV6 > 128).

    Parameters

    • range1: RangeObject

      First IP range (must have .first and .last arrays).

    • range2: RangeObject

      Second IP range (same version as range1).

    • Optionaloptions: IntersectOptions = {}

      Optional prefix length bounds and debug flag.

    Returns null | RangeObject

    A CIDR-aligned RangeObject covering both inputs, or null if the IP versions differ or the result violates constraints.

    If any option is not a number.

    If any option is out of bounds or if min > max.

  • Protected

    Converts an IP string or IP instance into a range object.

    Parameters

    • ip: string | IP

      IP/CIDR string or IP instance.

    • Optionaloptions: ParseOptions & {
          bitLen?: number;
      } = {}

      Optional parsing options for ip.

    Returns null | RangeObject

    Range object for the given IP, or null if invalid.

  • Protected

    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:

    • IPv4: 'x.x.x.x' or 'x.x.x.x/bitLen', where x = 0–255, bitLen = 0–32
    • IPv6: 'xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx' or shortened '::' forms, optionally with '/bitLen', where bitLen = 0–128

    Limitations:

    • Does not handle IPv4-mapped IPv6 addresses (e.g., ::ffff:192.168.0.1)

    Parameters

    • ipStr: string

      The string to parse.

    • Optionaloptions: {
          bitLen: undefined | number;
          suppressFullLengthCidr: undefined | boolean;
      } = {}

      Optional parsing options.

      • bitLen: undefined | number

        If provided, overrides any CIDR bit length in ipStr.

      • suppressFullLengthCidr: undefined | boolean

    Returns null | Parsed

    A parsed object with parts and optional bit length, or null if invalid.

    bitLen will be null if:

    • the input string does not contain a bit length, or
    • suppressFullLengthCidr is true, and the parsed or specified bit length is either 32 for IPv4 or 128 for IPv6.
  • Protected

    Parses and stringifies an IP string with optional filtering.

    Parameters

    • ipStr: string

      IP address or CIDR string to parse.

    • options: IPOptions

      Formatting options for output.

    Returns null | string

    Formatted IP string, or null if:

    • The input is invalid.
    • The address fails the conditionPredicate.
  • Protected

    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).

    Parameters

    • parts: number[]

      Array of decimal IP parts:

      • 4 elements for IPv4 (each 0–255)
      • 8 elements for IPv6 (each 0–65535)
    • bitLen: null | number

      Optional CIDR bit length (0–32 for IPv4, 0–128 for IPv6).

    Returns RangeObject

    Object with the first and last IPs in the range.

    If parts is not a valid IPv4 or IPv6 array.

  • Protected

    Converts an array of decimal IP parts into a string.

    Parameters

    • decimals: number[]

      Array of decimal IP parts.

    • suffix: string

      String to append after the address (e.g., CIDR /24 or empty string).

    • Optionaloptions: StringifyOptions = {}

      Formatting options.

    Returns string

    Formatted IP address string.

  • Protected

    Validates whether a string is a valid IP or CIDR address.

    • If allowCidr is true, CIDR suffixes are allowed.
    • If allowCidr is 'strict', the method returns a normalized CIDR string if the input is valid but not in canonical form.
    • If 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).

    Parameters

    • ipStr: string

      The IP or CIDR string to validate.

    • allowCidr: boolean | "strict"

      Whether to allow CIDRs, or require strict CIDR format.

    • Optionaloptions: Omit<IPOptions, "suppressFullLengthCidr"> = {}

      Optional formatting and parsing options.

    Returns string | boolean

    See above.

  • Returns an abbreviated representation of an IP string.

    Examples:

    Inaccurate CIDRs are corrected automatically:

    • Input: fd12:3456:789a:1:0:0:0:1/64
    • Output: fd12:3456:789a:1::/64

    Parameters

    • ipStr: string

      IP or CIDR string to abbreviate.

    • Optionaloptions: IPOptions = {}

      Optional formatting and parsing options, where format is coerced into "short".

    Returns null | string

    Abbreviated string, or null if:

    • The input string does not represent an IP address.
    • The parsed IP address does not meet the conditions specified by conditionPredicate.
  • Removes 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:

    • U+200E LEFT-TO-RIGHT MARK (LRM)
    • U+200F RIGHT-TO-LEFT MARK (RLM)
    • U+202A to U+202E (directional overrides)

    This logic mirrors cleanup done in MediaWiki core: TitleParser::splitTitleString

    Parameters

    • str: string

      Input string (may contain invisible bidi characters).

    Returns string

    Cleaned string, safe for IP parsing.

  • Checks whether a CIDR range contains the specified IP address.

    Parameters

    • cidrStr: string | IP

      The CIDR string or IP instance representing the containing range.

    • ipStr: string | IP

      The target IP address to check.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

    true if cidrStr contains ipStr, false if not, or null if either input is invalid.

  • Checks whether a CIDR range contains all of the IP addresses in the array.

    Parameters

    • cidrStr: string | IP

      The CIDR string or IP instance representing the containing range.

    • ipArr: (string | IP)[]

      An array of IP or CIDR strings or IP instances to test.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

    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.

  • Checks whether a CIDR range contains any of the IP addresses in the array.

    Parameters

    • cidrStr: string | IP

      The CIDR string or IP instance representing the containing range.

    • ipArr: (string | IP)[]

      An array of IP or CIDR strings or IP instances to test.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | number

    The index of the first match in ipArr, -1 if none match, or null if cidrStr is invalid.

  • Checks whether two IP addresses are equal.

    Parameters

    • ipStr1: string | IP

      The first IP address to compare.

    • ipStr2: string | IP

      The second IP address to compare.

    Returns null | boolean

    true if the IPs are equal, false if not, or null if either input is invalid.

  • Checks whether an IP address is equal to all addresses in a given array.

    Parameters

    • ipStr: string | IP

      The IP address to compare.

    • ipArr: (string | IP)[]

      An array of IP or CIDR strings or IP instances to compare against.

    Returns null | boolean

    true if all addresses are equal to ipStr, false otherwise, or null if ipStr is invalid or ipArr is not an array or an empty array.

  • Checks whether an IP address is equal to any address in a given array.

    Parameters

    • ipStr: string | IP

      The IP address to compare.

    • ipArr: (string | IP)[]

      An array of IP or CIDR strings or IP instances to check against.

    Returns null | number

    The index of the first match in ipArr, -1 if none match, or null if ipStr is invalid.

  • Returns the narrowest CIDR range that encompasses two IP addresses or subnets, provided they are of the same version (both IPv4 or both IPv6).

    Parameters

    • ip1: string | IP

      First IP address or CIDR to intersect.

    • ip2: string | IP

      Second IP address or CIDR to intersect.

    • Optionaloptions: IntersectOptions = {}

      Optional prefix length constraints and verbosity flag.

    Returns null | IP

    An IP instance representing the narrowest common range, or null if:

    • Either ip1 or ip2 is invalid.
    • The IP versions differ (e.g., one is IPv4 and the other is IPv6).
  • Checks whether the input is a valid CIDR (either IPv4 or IPv6).

    Parameters

    • ipStr: string

      The CIDR string to check.

    • Optionalmode: "strict"

      Require strict CIDR formatting if 'strict' is passed.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether the input is a valid IP or CIDR address.

    Parameters

    • ipStr: string

      The IP or CIDR string to check.

    • OptionalallowCidr: boolean | "strict" = false

      Whether to allow CIDRs, or require strict CIDR format.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether the input is a valid IPv4 address or IPv4 CIDR.

    Parameters

    • ipStr: string

      The IPv4 or IPv4 CIDR string to check.

    • OptionalallowCidr: boolean | "strict" = false

      Whether to allow CIDRs, or require strict CIDR format.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether the input is a valid IPv4 CIDR.

    Parameters

    • ipStr: string

      The IPv4 CIDR string to check.

    • Optionalmode: "strict"

      Require strict CIDR formatting if 'strict' is passed.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether the input is a valid IPv6 address or IPv6 CIDR.

    Parameters

    • ipStr: string

      The IPv6 or IPv6 CIDR string to check.

    • OptionalallowCidr: boolean | "strict" = false

      Whether to allow CIDRs, or require strict CIDR format.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether the input is a valid IPv6 CIDR.

    Parameters

    • ipStr: string

      The IPv6 CIDR string to check.

    • Optionalmode: "strict"

      Require strict CIDR formatting if 'strict' is passed.

    • Optionaloptions: StringifyOptions = {}

      Formatting options for corrected CIDRs.

    Returns string | boolean

    Returns true if valid, false if invalid, or a normalized CIDR string.

  • Checks whether a given IP address is within all CIDR ranges in the array.

    Parameters

    • ipStr: string | IP

      The IP address to evaluate.

    • cidrArr: (string | IP)[]

      An array of CIDR strings or IP instances to check against.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

    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.

  • Checks whether a given IP address is within any of the CIDR ranges in the array.

    Parameters

    • ipStr: string | IP

      The IP address to evaluate.

    • cidrArr: (string | IP)[]

      An array of CIDR strings or IP instances to check against.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | number

    The index of the first matching CIDR in the array, -1 if none match, or null if ipStr is invalid.

  • Checks whether a given IP address is within the CIDR range of another.

    Parameters

    • ipStr: string | IP

      The target IP address to evaluate.

    • cidrStr: string | IP

      The CIDR string or IP instance representing the range.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

    true if ipStr is within the range of cidrStr, false if not, or null if either input is invalid.

  • Returns 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:

    • Input: fd12:3456:789a:1:0:0:0:1/64
    • Output: fd12:3456:789a:0001:0000:0000:0000:0000/64

    Parameters

    • ipStr: string

      IP or CIDR string to expand.

    • Optionaloptions: IPOptions = {}

      Optional formatting and parsing options, where format is coerced into "long".

    Returns null | string

    Expanded string, or null if:

    • The input string does not represent an IP address.
    • The parsed IP address does not meet the conditions specified by conditionPredicate.
  • Returns a sanitized representation of an IP string.

    Examples:

    Inaccurate CIDRs are corrected automatically:

    • Input: fd12:3456:789a:1::1/64
    • Output: fd12:3456:789a:1:0:0:0:0/64

    Parameters

    • ipStr: string

      IP or CIDR string to sanitize.

    • Optionaloptions: IPOptions = {}

      Optional formatting and parsing options, where format is coerced into "default".

    Returns null | string

    Sanitized string, or null if:

    • The input string does not represent an IP address.
    • The parsed IP address does not meet the conditions specified by conditionPredicate.