cidr-block - v2.1.1
    Preparing search index...

    Class Ipv4Address

    Represents an IPv4 address with utility methods for manipulation and comparison.

    While you can instantiate this class directly, it is recommended to use the ipv4.address shorthand method from the ipv4 namespace instead.

    import { ipv4 } from 'cidr-block';

    // Recommended: use the ipv4 namespace shorthand
    const addr1 = ipv4.address("192.168.1.1");
    const addr2 = ipv4.address(3232235777);
    const addr3 = ipv4.address([192, 168, 1, 1]);
    import { ipv4 } from 'cidr-block';

    const addr = ipv4.address("192.168.1.1");
    addr.toString(); // "192.168.1.1"
    addr.toNumber(); // 3232235777
    addr.octets(); // [192, 168, 1, 1]
    addr.toBinaryString(); // "11000000.10101000.00000001.00000001"
    import { ipv4 } from 'cidr-block';

    const addr = ipv4.address("192.168.1.1");
    addr.equals("192.168.1.1"); // true
    addr.isGreaterThan("192.168.1.0"); // true
    addr.isLessThan("192.168.1.2"); // true
    import { ipv4 } from 'cidr-block';

    const addr = ipv4.address("192.168.1.1");
    addr.nextAddress()?.toString(); // "192.168.1.2"
    addr.previousAddress()?.toString(); // "192.168.1.0"
    import { ipv4 } from 'cidr-block';

    ipv4.address("127.0.0.1").isLoopbackAddress(); // true
    ipv4.address("10.0.0.1").isPrivateAddress(); // true
    ipv4.address("169.254.1.1").isLocalLinkAddress(); // true
    ipv4.address("224.0.0.1").isMulticastAddress(); // true
    Index

    Constructors

    Methods

    • Compares two IPv4 addresses for equality.

      Parameters

      Returns boolean

      true if both IPv4 addresses are equal

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.equals("192.168.1.1"); // true
      addr.equals([192, 168, 1, 1]); // true
      addr.equals(ipv4.address("10.0.0.1")); // false
    • Checks if there is a next sequential IPv4 address. This would only return false if the current address is the maximum possible value (255.255.255.255).

      Returns boolean

      true if there is a next IPv4 address.

      import { ipv4 } from 'cidr-block';

      ipv4.address("192.168.1.1").hasNextAddress(); // true
      ipv4.address("255.255.255.255").hasNextAddress(); // false
    • Checks if there is a previous sequential IPv4 address. This would only return false if the current address is the minimum possible value (0.0.0.0).

      Returns boolean

      true if there is a previous IPv4 address.

      import { ipv4 } from 'cidr-block';

      ipv4.address("192.168.1.1").hasPreviousAddress(); // true
      ipv4.address("0.0.0.0").hasPreviousAddress(); // false
    • Compares if this IPv4 address is greater than another IPv4 address.

      Parameters

      Returns boolean

      true if this IPv4 address is greater than the other IPv4 address

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.isGreaterThan("192.168.1.0"); // true
      addr.isGreaterThan("192.168.1.1"); // false
      addr.isGreaterThan("192.168.1.2"); // false
    • Compares if this IPv4 address is greater than or equal to another IPv4 address.

      Parameters

      Returns boolean

      true if this IPv4 address is greater than or equal to the other IPv4 address

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.isGreaterThanOrEqual("192.168.1.0"); // true
      addr.isGreaterThanOrEqual("192.168.1.1"); // true
      addr.isGreaterThanOrEqual("192.168.1.2"); // false
    • Compares if this IPv4 address is less than another IPv4 address.

      Parameters

      Returns boolean

      true if this IPv4 address is less than the other IPv4 address

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.isLessThan("192.168.1.2"); // true
      addr.isLessThan("192.168.1.1"); // false
      addr.isLessThan("192.168.1.0"); // false
    • Compares if this IPv4 address is less than or equal to another IPv4 address.

      Parameters

      Returns boolean

      true if this IPv4 address is less than or equal to the other IPv4 address

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.isLessThanOrEqual("192.168.1.2"); // true
      addr.isLessThanOrEqual("192.168.1.1"); // true
      addr.isLessThanOrEqual("192.168.1.0"); // false
    • Checks if the IPv4 address is a local-link address (in the CIDR of 169.254.0.0/16).

      Returns boolean

      true if the IPv4 address is a local-link address.

      import { ipv4 } from 'cidr-block';

      ipv4.address("169.254.1.1").isLocalLinkAddress(); // true
      ipv4.address("169.254.255.255").isLocalLinkAddress(); // true
      ipv4.address("192.168.1.1").isLocalLinkAddress(); // false
    • Checks if the IPv4 address is a loopback address (in the CIDR of 127.0.0.0/8)

      Returns boolean

      true if the IPv4 address is a loopback address (example: 127.0.0.1 is true)

      import { ipv4 } from 'cidr-block';

      ipv4.address("127.0.0.1").isLoopbackAddress(); // true
      ipv4.address("127.255.0.1").isLoopbackAddress(); // true
      ipv4.address("192.168.1.1").isLoopbackAddress(); // false
    • Checks if the IPv4 address is a multicast address (between 224.0.0.0 to 239.255.255.255).

      Returns boolean

      true if the IPv4 address is a multicast address.

      import { ipv4 } from 'cidr-block';

      ipv4.address("224.0.0.1").isMulticastAddress(); // true
      ipv4.address("239.255.255.255").isMulticastAddress(); // true
      ipv4.address("192.168.1.1").isMulticastAddress(); // false
    • Checks if the IPv4 address is a private address.

      This is based on RFC 1918, which defines the following private address ranges:

      • 10.0.0.0/8
      • 172.16.0.0/12
      • 192.168.0.0/16

      Returns boolean

      true if the IPv4 address is in any of the private address ranges.

      import { ipv4 } from 'cidr-block';

      ipv4.address("10.0.0.1").isPrivateAddress(); // true
      ipv4.address("172.16.0.1").isPrivateAddress(); // true
      ipv4.address("192.168.1.1").isPrivateAddress(); // true
      ipv4.address("8.8.8.8").isPrivateAddress(); // false
    • Gets the next sequential IPv4 address.

      Returns Ipv4Address | undefined

      The next IPv4 address, or undefined if the current address is the maximum possible value.

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.nextAddress()?.toString(); // "192.168.1.2"

      const maxAddr = ipv4.address("255.255.255.255");
      maxAddr.nextAddress(); // undefined
    • Gets the octets of the IPv4 address.

      Returns Ipv4AddressOctets

      An array of four numbers representing the octets of the IPv4 address.

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.octets(); // [192, 168, 1, 1]
    • Gets the previous sequential IPv4 address.

      Returns Ipv4Address | undefined

      The previous IPv4 address, or undefined if the current address is the minimum possible value.

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.previousAddress()?.toString(); // "192.168.1.0"

      const minAddr = ipv4.address("0.0.0.0");
      minAddr.previousAddress(); // undefined
    • Returns the binary string representation of the IPv4 address.

      Returns string

      The IPv4 address as a binary string in dotted-decimal notation (example: "11000000.10111011.00000000.00000001").

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.toBinaryString(); // "11000000.10101000.00000001.00000001"
    • Converts the IPv4 address to its numeric representation.

      Returns number

      The IPv4 address as a number.

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address("192.168.1.1");
      addr.toNumber(); // 3232235777
    • Returns the string representation of the IPv4 address.

      Returns `${number}.${number}.${number}.${number}`

      The IPv4 address as a string in dotted-decimal notation (example: "192.187.0.1").

      import { ipv4 } from 'cidr-block';

      const addr = ipv4.address([10, 0, 0, 1]);
      addr.toString(); // "10.0.0.1"