Skip to content

Class: IP ​

The IP address toolkit for both versions: validate, convert and compare IPv4 and IPv6 addresses and CIDR blocks. Its static methods try an input as IPv4 first, then as IPv6; IPv4 and IPv6 handle one version, with the methods specific to it (the subnet masks, the private ranges, the expanded and compressed forms). The class is not instantiated. The invalid inputs give false rather than throwing.

Example ​

typescript
import {IP} from 'lakutata/utils'

//An allowlist of networks, of both versions
const allowed: string[] = ['10.0.0.0/8', '2001:db8::/32']
const remote: string = '2001:db8::17'
console.log(IP.isValidIP(remote), allowed.some((cidr: string): boolean => IP.contains(cidr, remote)))
//true true
console.log(IP.ip2long('192.168.0.1'), IP.long2ip(1n), IP.isConflict(['10.0.0.0/8', '10.1.0.0/16']))
//3232235521 ::1 true

Constructors ​

Constructor ​

new IP(): IP

Returns ​

IP

Methods ​

contains() ​

static contains(cidr, ip): boolean

Tell whether an address is in a CIDR block, the network and broadcast addresses included. An IPv4-mapped IPv6 address is in the IPv4 blocks of its IPv4 address ('::ffff:192.168.1.5' is in '192.168.1.0/24', as a dual-stack server sees an IPv4 client); an IPv4 address is not in an IPv6 block.

Parameters ​

cidr ​

string

The CIDR block, IPv4 ('192.168.1.0/24') or IPv6 ('2001:db8::/64'); see IPv4.isCIDR and IPv6.isCIDR.

ip ​

string

The address, IPv4 or IPv6.

Returns ​

boolean

true when the address is in the block, false otherwise or when either is invalid or not a string.


ip2long() ​

static ip2long(ip): number | bigint | false

Convert an address into its integer form: a number for IPv4 ('192.168.0.1' gives 3232235521), a bigint for IPv6 ('::ffff:9999' gives 4294941081n).

Parameters ​

ip ​

string

The address, IPv4 or IPv6.

Returns ​

number | bigint | false

The integer form ('0.0.0.0' gives 0), or false when the address is invalid.


isCIDR() ​

static isCIDR(cidr): boolean

Tell whether a string is a valid CIDR block, IPv4 (a prefix length from 0 to 32) or IPv6 (from 0 to 128); see IPv4.isCIDR and IPv6.isCIDR.

Parameters ​

cidr ​

string

The string to test ('192.168.1.0/24', '2001:db8::/32').

Returns ​

boolean

true when it is a valid CIDR block, false otherwise or when it is not a string.


isConflict() ​

static isConflict(cidrs): boolean

Tell whether two CIDR blocks of the same version in a list overlap. The blocks of different versions never conflict, and the invalid entries are ignored.

Parameters ​

cidrs ​

string[]

The CIDR blocks, of either version (['192.168.1.0/24', '192.168.1.0/28', '2001:db8::/122']).

Returns ​

boolean

true when two blocks overlap, false otherwise or when the list is empty or not an array.


isValidIP() ​

static isValidIP(ip): boolean

Tell whether a string is a valid IPv4 or IPv6 address; see IPv4.isValidIP and IPv6.isValidIP.

Parameters ​

ip ​

string

The string to test ('192.168.1.99', 'f16c:f7ec:cfa2:e1c5:9a3c:cb08:801f:36b8').

Returns ​

boolean

true when it is a valid address of either version, false otherwise or when it is not a string.


long2ip() ​

static long2ip(ip): string | false

Convert the integer form of an address into its text: a number gives an IPv4 address in dotted decimal notation (3232235521 gives '192.168.0.1'), a bigint an IPv6 address in compressed form (1n gives '::1').

Parameters ​

ip ​

number | bigint

The integer form: a number from 0 to 4294967295 (its fraction dropped), or a bigint from 0n to 2n ** 128n - 1n.

Returns ​

string | false

The address, or false when the integer is out of range or neither a number nor a bigint.

Released under the MIT License.