Class IP

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:

const ip = IP.newFromText('fd12:3456:789a:1::1');
if (!ip) return;
console.log(ip.stringify()); // fd12:3456:789a:1:0:0:0:1

Hierarchy

  • IPBase
    • IP

Properties

bitLen: number
first: number[]
isCidr: boolean
last: number[]

Accessors

  • get version(): 4 | 6
  • Returns the IP version as a number.

    Returns 4 | 6

    The IP version: 4 for IPv4 or 6 for IPv6.

Methods

  • 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'.

    Parameters

    • Optionalcapitalize: boolean = false

      Whether to capitalize the output.

    Returns string

    A properly formatted string representation of the IP or CIDR.

  • Checks whether the CIDR range associated with this instance contains the specified IP address.

    Parameters

    • ipStr: string | IP

      The target IP address to check.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

    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.

    Parameters

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

    Parameters

    • ipArr: (string | IP)[]

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

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns number

    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.

    Parameters

    • ipStr: string | IP

      The IP address to compare.

    Returns null | boolean

    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.

    Parameters

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

    Parameters

    • ipArr: (string | IP)[]

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

    Returns number

    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.

    Returns number

    The bit length as a number.

  • Gets a copy of the internal CIDR-related properties.

    Returns RangeObject

    An object containing first, last, bitLen, and isCidr.

  • Gets range information of the IP instance.

    Parameters

    • OptionalgetInstance: false

      Whether to get the start and end IP addresses as IP instances.

    • Optionaloptions: StringifyOptions

      Optional formatting options for the cidr, first, and last properties.

    Returns {
        bitLen: number;
        cidr: string;
        first: string;
        last: string;
    }

    • bitLen: number
    • cidr: string
    • first: string
    • last: string
  • Gets range information of the IP instance.

    Parameters

    • getInstance: true

      Whether to get the start and end IP addresses as IP instances.

    • Optionaloptions: StringifyOptions

      Optional formatting options for the cidr property.

    Returns {
        bitLen: number;
        cidr: string;
        first: IP;
        last: IP;
    }

    • bitLen: number
    • cidr: string
    • first: IP
    • last: IP
  • Gets the IP version as a string in the format IPv4 or IPv6.

    Returns "IPv4" | "IPv6"

    A string representation of the IP version.

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

    Parameters

    • ip: string | IP

      IP address or CIDR string to intersect with this instance.

    • Optionaloptions: IntersectOptions = {}

      Optional prefix length constraints and verbosity flag.

    Returns null | IP

    A new IP instance representing the narrowest common CIDR range, or null if:

    • The input ip is invalid.
    • The IP versions differ.
  • Checks whether the current instance represents a CIDR address.

    Returns boolean

    A boolean indicating whether the current instance represents a CIDR address.

  • Checks whether the current instance represents an IPv4 address.

    Parameters

    • OptionalallowCidr: boolean = false

      Whether to allow a CIDR address.

    Returns boolean

    A boolean indicating whether the current instance represents an IPv4 address.

  • Checks whether the current instance represents an IPv4 CIDR address.

    Returns boolean

    A boolean indicating whether the current instance represents an IPv4 CIDR address.

  • Checks whether the current instance represents an IPv6 address.

    Parameters

    • OptionalallowCidr: boolean = false

      Whether to allow a CIDR address.

    Returns boolean

    A boolean indicating whether the current instance represents an IPv6 address.

  • Checks whether the current instance represents an IPv6 CIDR address.

    Returns boolean

    A boolean indicating whether the current instance represents an IPv6 CIDR address.

  • Checks whether the IP address associated with this instance is within all CIDR ranges in the array.

    Parameters

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

    Parameters

    • cidrArr: (string | IP)[]

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

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns number

    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.

    Parameters

    • cidrStr: string | IP

      The CIDR string or IP instance representing the range.

    • Optionaloptions: CompareOptions = {}

      Options for comparing the input IPs.

    Returns null | boolean

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

    Parameters

    • Optionalcapitalize: boolean = false

      Whether to capitalize the output.

    Returns string

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

    Parameters

    • Optionalcapitalize: boolean = false

      Whether to capitalize the output.

    Returns string

    A properly formatted string representation of the IP or CIDR.

  • Returns the stringified form of this IP or CIDR block.

    Parameters

    • Optionaloptions: StringifyOptions = {}

      Optional formatting options. If omitted, a default "sanitized" format will be used.

    Returns string

    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.

    Returns string

    A stringified representation of the IP.

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

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

  • Initializes an IP instance from a string and a range (i.e., a prefix length).

    Parameters

    • ipStr: string | IP

      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.

    • range: number

      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:

      • If 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
      • If 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
    • Optionaloptions: ParseOptions = {}

      Optional parsing options for the input.

    Returns null | IP

    A new IP instance if parsing succeeds; otherwise, null if the input or range is invalid.

    If range is not an integer.

  • Initializes an IP instance from a string.

    Parameters

    • ipStr: string | IP

      An IP- or CIDR-representing string.

      If an IP instance is passed, it will be cloned, unless prevented by options.

    • Optionaloptions: ParseOptions = {}

      Optional parsing options for the input string.

    Returns null | IP

    A new IP instance if parsing succeeds, or null if the input is invalid.