cidrLastAddress<T extends Cidr>(cidr: T): T extends Cidrv6 ? bigint : number
Returns the last address of a CIDR block.
Dispatches to cidrv4LastAddress or cidrv6LastAddress based on the address type. Together with cidrFirstAddress this gives the numeric bounds of a block without knowing its version — the pair sorting, range comparison, and interval arithmetic want.
Note the versions differ in what the last address means: for IPv4 it is the directed broadcast address and is not assignable (cidrv4BroadcastAddress), while IPv6 has no broadcast address and the last address is assignable.
Example 1
Example 1
import { assertEquals } from "@std/assert"; import { cidrLastAddress, parseCidr } from "@hertzg/ip/cidr"; import { stringifyAddress } from "@hertzg/ip/address"; assertEquals(stringifyAddress(cidrLastAddress(parseCidr("192.168.1.0/24"))), "192.168.1.255"); assertEquals(stringifyAddress(cidrLastAddress(parseCidr("2001:db8::/120"))), "2001:db8::ff");
The bounds of a block, without knowing its version
The bounds of a block, without knowing its version
import { assertEquals } from "@std/assert"; import { cidrFirstAddress, cidrLastAddress, parseCidr } from "@hertzg/ip/cidr"; import { stringifyAddress } from "@hertzg/ip/address"; const v4 = parseCidr("10.0.0.0/29"); assertEquals(stringifyAddress(cidrFirstAddress(v4)), "10.0.0.0"); assertEquals(stringifyAddress(cidrLastAddress(v4)), "10.0.0.7"); const v6 = parseCidr("fd00::/125"); assertEquals(stringifyAddress(cidrFirstAddress(v6)), "fd00::"); assertEquals(stringifyAddress(cidrLastAddress(v6)), "fd00::7");
T extends Cidr
cidr: T
The CIDR block