Class IPUtil

The IPUtil class. Unlike the IP class, this class provides several static methods that can be used to perform validations on an IP or CIDR address just once, or on varying IP or CIDR addresses.

Hierarchy

  • IPBase
    • IPUtil

Methods

  • Abbreviate an IP-representing string. For example:

    • 192.168.0.1 (for IPv4 addresses, same as IPUtil.sanitize)
    • fd12:3456:789a:1::

    Note that inaccurate CIDRs will be corrected:

    • input: fd12:3456:789a:1:0:0:0:1/64
    • output: fd12:3456:789a:1::/64

    Parameters

    • ipStr: string
    • Optionalcapitalize: boolean

      Whether to capitalize the output, which defaults to false.

    • OptionalconditionPredicate: ConditionPredicate

      Optional IP address conditions to perform stringification.

    Returns null | string

    null if:

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

    Check the equality of two IP addresses.

    Parameters

    • ipObj: RangeObject

      An object of arrays of the IP parts in decimals.

    • ipStr: string | IP

      An IP- or CIDR-representing string, or an IP instance.

    Returns null | boolean

    null if ipStr does not represent an IP address.

  • Return a trimmed string from which all occurrences of unicode bidi characters are removed.

    "Unicode bidi characters" are special characters shown as red dots in WikiEditor, which can slip into cut-and-pasted strings. When we evaluate whether a string represents an IP address, these can cause serious trouble.

    Parameters

    • str: string

    Returns string

    MediaWikiTitleCodec::splitTitleString in MediaWiki core

  • Protected

    Compare two ranges to check their inclusion relationship.

    Parameters

    • ip1: RangeObject

      An object of arrays of the IP parts in decimals.

    • ip2: string | IP
    • comparator: "<" | ">"

      Which of ip1 and ip2 is expected to be broader.

    Returns null | boolean

    null if ip2 does not represent an IP address.

  • Evaluate whether the IP address associated with cidrStr contains that associated with ipStr.

    Parameters

    • cidrStr: string | IP
    • ipStr: string | IP

    Returns null | boolean

    null if any of the two input strings does not represent an IP address.

  • Evaluate whether the IP address associated with cidrStr contains all IP addresses in the ipArr array.

    Parameters

    • cidrStr: string | IP
    • ipArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | boolean

    null if:

    • cidrStr does not represent an IP address.
    • ipArr is not an array or an empty array.
  • Evaluate whether the IP address associated with cidrStr contains any IP address in the ipArr array.

    Parameters

    • cidrStr: string | IP
    • ipArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | number

    The index number of the first match in the ipArr array, or -1 if there is no match. null will be returned if cidrStr does not represent an IP address.

  • Evaluate whether the IP address associated with ipStr1 equals that associated with ipStr2.

    Parameters

    • ipStr1: string | IP
    • ipStr2: string | IP

    Returns null | boolean

    null if any of the two input strings does not represent an IP address.

  • Evaluate whether the IP address associated with ipStr equals all IP addresses in the ipArr array.

    Parameters

    • ipStr: string | IP
    • ipArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | boolean

    null if:

    • ipStr does not represent an IP address.
    • ipArr is not an array or an empty array.
  • Evaluate whether the IP address associated with ipStr equals any IP address in the ipArr array.

    Parameters

    • ipStr: string | IP
    • ipArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | number

    The index number of the first match in the ipArr array, or -1 if there is no match. null will be returned if ipStr does not represent an IP address.

  • Protected

    Given an IP string or instance, parse it into an object of arrays of decimals that represent the parts of the first and last IPs.

    Parameters

    • ip: string | IP

    Returns null | RangeObject

    null if the input string does not represent an IP address.

  • Evaluate whether a string represents a CIDR.

    Parameters

    • ipStr: string

    Returns boolean

  • Evaluate whether a string represents a CIDR, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • mode: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether a string represents an IP address.

    Parameters

    • ipStr: string
    • OptionalallowCidr: boolean

      Whether to allow CIDRs, which defaults to false.

    Returns boolean

  • Evaluate whether a string represents an IP address, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • allowCidr: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether a string represents an IPv4 address.

    Parameters

    • ipStr: string
    • OptionalallowCidr: boolean

      Whether to allow CIDRs, which defaults to false.

    Returns boolean

  • Evaluate whether a string represents an IPv4 address, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • allowCidr: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether a string represents an IPv4 CIDR.

    Parameters

    • ipStr: string

    Returns boolean

  • Evaluate whether a string represents an IPv4 CIDR, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • mode: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether a string represents an IPv6 address.

    Parameters

    • ipStr: string
    • OptionalallowCidr: boolean

      Whether to allow CIDRs, which defaults to false.

    Returns boolean

  • Evaluate whether a string represents an IPv6 address, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • allowCidr: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether a string represents an IPv6 CIDR.

    Parameters

    • ipStr: string

    Returns boolean

  • Evaluate whether a string represents an IPv6 CIDR, with the strict CIDR validation mode on.

    Parameters

    • ipStr: string
    • mode: "strict"
    • Optionaloptions: StringifyOptions

      Optional specifications of how the output CIDR string should be formatted.

    Returns string | boolean

  • Evaluate whether the IP address associated with ipStr is within all IP ranges in the cidrArr array.

    Parameters

    • ipStr: string | IP
    • cidrArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | boolean

    null if:

    • ipStr does not represent an IP address.
    • cidrArr is not an array or an empty array.
  • Evaluate whether the IP address associated with ipStr is within any IP range in the cidrArr array.

    Parameters

    • ipStr: string | IP
    • cidrArr: (string | IP)[]

      An array of IP- or CIDR-representing strings or IP instances.

    Returns null | number

    The index number of the first match in the cidrArr array, or -1 if there is no match. null will be returned if ipStr does not represent an IP address.

  • Evaluate whether the IP address associated with ipStr is within that associated with cidrStr.

    Parameters

    • ipStr: string | IP
    • cidrStr: string | IP

    Returns null | boolean

    null if any of the two input strings does not represent an IP address.

  • Lengthen an IP-representing string. For example:

    • 192.168.000.001
    • fd12:3456:789a:0001:0000:0000:0000:0000

    Note that inaccurate CIDRs will be corrected:

    • input: fd12:3456:789a:1:0:0:0:1/64
    • output: fd12:3456:789a:0001:0000:0000:0000:0000/64

    Parameters

    • ipStr: string
    • Optionalcapitalize: boolean

      Whether to capitalize the output, which defaults to false.

    • OptionalconditionPredicate: ConditionPredicate

      Optional IP address conditions to perform stringification.

    Returns null | string

    null if:

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

    Change the casing of a string.

    Parameters

    • str: string
    • Optionalupper: boolean = false

      Whether to capitalize the output, which defaults to false.

    Returns string

    The original string if upper isn't true.

  • Protected

    Parse a string that potentially represents an IP or CIDR address.

    Parameters

    • ipStr: string
    • OptionalbitLen: number

      An optional bit length of the IP address.

    Returns null | Parsed

    A parsed object, or null if:

    • ipStr is not a string.
    • ipStr does not represent an IP address.
    • ipStr contains an invalid bit length for a CIDR.
  • Protected

    Parse an IP string into an array and convert back into a string.

    Parameters

    Returns null | string

    null if:

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

    Get the start and end IP addresses for the range of bitLen as an object of arrays of decimals.

    Parameters

    • parts: number[]
    • bitLen: null | number

    Returns RangeObject

  • Sanitize an IP-representing string. For example:

    • 192.168.0.1 (for IPv4 addresses, same as IPUtil.abbreviate)
    • fd12:3456:789a:1:0:0:0:0

    Note that inaccurate CIDRs will be corrected:

    • input: fd12:3456:789a:1::1/64
    • output: fd12:3456:789a:1:0:0:0:0/64

    Parameters

    • ipStr: string
    • Optionalcapitalize: boolean

      Whether to capitalize the output, which defaults to false.

    • OptionalconditionPredicate: ConditionPredicate

      Optional IP address conditions to perform stringification.

    Returns null | string

    null if:

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

    Stringify an array of IP parts in decimals.

    Parameters

    Returns string

  • Validate a string as an IP address under certain conditions.

    Parameters

    Returns boolean

  • Validate a string as an IP address under certain conditions.

    Parameters

    Returns string | boolean