Skip to content

Class: Subnet<T> ​

Defined in: Subnet.ts:24

Represents an address range subnetwork of Internet Protocol (IP) addresses.

Type Parameters ​

T ​

T extends IP

IP address family.

Implements ​

Constructors ​

Constructor ​

new Subnet<T>(address, prefix): Subnet<T>

Defined in: Subnet.ts:56

Creates a new subnet instance.

Parameters ​

address ​

T

IP address.

prefix ​

number

Bit length of the subnet prefix.

Returns ​

Subnet<T>

Throws ​

RangeError If the prefix is greater than the IP address family bit length, or if negative.

Properties ​

address ​

readonly address: T

Defined in: Subnet.ts:35

Subnet address (i.e. the first IP address of the network).


prefix ​

readonly prefix: number

Defined in: Subnet.ts:40

Bit length of the subnet prefix.


IPV4_MAPPED_IPV6 ​

readonly static IPV4_MAPPED_IPV6: Subnet<IP>

Defined in: Subnet.ts:28

Subnet for IPv4-mapped IPv6 addresses (::ffff:0.0.0.0/96).

Methods ​

[iterator]() ​

[iterator](): Iterator<T>

Defined in: Subnet.ts:222

Iterates all IP addresses in this subnet.

NOTE: This can be slow for large subnets.

Returns ​

Iterator<T>

Implementation of ​

Network.[iterator]


addresses() ​

addresses(): IterableIterator<T>

Defined in: Subnet.ts:210

Iterates all IP addresses in this subnet.

NOTE: This can be slow for large subnets.

Returns ​

IterableIterator<T>


at() ​

Call Signature ​

at(index): T

Defined in: Subnet.ts:150

Returns the address at the specific index.

Parameters ​
index ​

0 | 0n | -1 | -1n

Zero-based index of the address to be returned. Negative index counts from the end of the subnet.

Returns ​

T

The address in the subnet matching the given index. Always returns undefined if index < -size() or index >= size().

Call Signature ​

at(index): T | undefined

Defined in: Subnet.ts:159

Returns the address at the specific index.

Parameters ​
index ​

bigint

Zero-based index of the address to be returned. Negative index counts from the end of the subnet.

Returns ​

T | undefined

The address in the subnet matching the given index. Always returns undefined if index < -size() or index >= size().

Call Signature ​

at(index): T | undefined

Defined in: Subnet.ts:168

Returns the address at the specific index.

Parameters ​
index ​

number

Zero-based index of the address to be returned. Negative index counts from the end of the subnet.

Returns ​

T | undefined

The address in the subnet matching the given index. Always returns undefined if index < -size() or index >= size().


canMerge() ​

canMerge(subnet): boolean

Defined in: Subnet.ts:266

Checks whether another subnet can be merged with this subnet.

Parameters ​

subnet ​

Subnet<T>

Subnet to check.

Returns ​

boolean


contains() ​

contains(address): boolean

Defined in: Subnet.ts:184

Determines whether the provided address is contained within this subnet.

Parameters ​

address ​

T

IP address to check.

Returns ​

boolean

Implementation of ​

Network.contains


containsSubnet() ​

containsSubnet(subnet): boolean

Defined in: Subnet.ts:194

Determines whether the provided subnet is fully contained within this subnet.

Parameters ​

subnet ​

Subnet<T>

Subnet to check.

Returns ​

boolean


equals() ​

equals(subnet): boolean

Defined in: Subnet.ts:371

Checks if the given subnets are equal.

Parameters ​

subnet ​

Subnet<IP>

Subnet to compare.

Returns ​

boolean


isAdjacent() ​

isAdjacent(subnet): boolean

Defined in: Subnet.ts:250

Checks if this subnet is adjacent to another subnet.

Parameters ​

subnet ​

Subnet<T>

Subnet to check.

Returns ​

boolean

Throws ​

TypeError If the provided subnet is not of the same family.


merge() ​

merge(subnet): Subnet<T>

Defined in: Subnet.ts:284

Creates a larger subnet by merging with an adjacent subnet of the same family and size.

Parameters ​

subnet ​

Subnet<T>

Subnet to merge with.

Returns ​

Subnet<T>

Throws ​

TypeError If the subnet is not of the same family or size.

Throws ​

RangeError If the subnet is not adjacent to this subnet.


netmask() ​

netmask(): bigint

Defined in: Subnet.ts:131

Returns the network mask of this subnet.

Returns ​

bigint


set() ​

set(): Set<T>

Defined in: Subnet.ts:231

Creates a set containing all IP addresses in this subnet.

NOTE: This can be slow for large subnets.

Returns ​

Set<T>


size() ​

size(): bigint

Defined in: Subnet.ts:201

Returns the exact number of addresses in this subnet.

Returns ​

bigint

Implementation of ​

Network.size


split() ​

split(prefix): Subnet<T>[]

Defined in: Subnet.ts:309

Splits this subnet into as many subnets of the specified prefix length as possible.

Parameters ​

prefix ​

number

Prefix length of the resulting subnets.

Returns ​

Subnet<T>[]

Throws ​

RangeError If the prefix is smaller than the current prefix, or over the IP address family bit length.


subtract() ​

subtract(subnet): Subnet<T>[]

Defined in: Subnet.ts:336

Subtracts a subnet from this subnet.

Parameters ​

subnet ​

Subnet<T>

Subnet to exclude.

Returns ​

Subnet<T>[]

An array of subnets representing the portions of this subnet that do not overlap with the given subnet. Returns an empty array if the given subnet fully covers this subnet.

Throws ​

TypeError If the subnet is not of the same family.


toString() ​

toString(): string

Defined in: Subnet.ts:240

Returns the string representation of this subnet in CIDR notation.

Returns ​

string

Example ​

ts
"203.0.113.0/24"

wildcard() ​

wildcard(): bigint

Defined in: Subnet.ts:139

Returns the wildcard (host) mask—the inverse of the netmask.

Returns ​

bigint


fromCIDR() ​

static fromCIDR<T>(cidr): Subnet<T>

Defined in: Subnet.ts:76

Creates a subnet from a string in CIDR notation.

Type Parameters ​

T ​

T extends IP = IP

Parameters ​

cidr ​

string

String in CIDR notation.

Returns ​

Subnet<T>

Throws ​

RangeError If the address or prefix is invalid.


range() ​

static range<T>(start, end): Subnet<T>[]

Defined in: Subnet.ts:94

Creates an array of subnets to represent an arbitrary range of IP addresses.

Type Parameters ​

T ​

T extends IP

Parameters ​

start ​

T

Starting IP address.

end ​

T

Ending IP address.

Returns ​

Subnet<T>[]

Released under the LGPL-3.0 licence.