cidrFirstAddress<T extends Cidr>(cidr: T): T extends Cidrv6 ? bigint : number
Returns the first address of a CIDR block.
Dispatches to cidrv4FirstAddress or cidrv6FirstAddress based on the address type. Masking a non-canonical block through this is the version-agnostic way to canonicalize it.
There is deliberately no universal cidrFirstUsableAddress: the addresses
an operator may assign differ per version, and IPv6 has no library-wide
rule to apply. The usable-address vocabulary is IPv4-only — see
cidrv4FirstUsableAddress.
Example 1
Example 1
import { assertEquals } from "@std/assert"; import { cidrFirstAddress, parseCidr } from "@hertzg/ip/cidr"; import { stringifyAddress } from "@hertzg/ip/address"; assertEquals(stringifyAddress(cidrFirstAddress(parseCidr("192.168.1.0/24"))), "192.168.1.0"); assertEquals(stringifyAddress(cidrFirstAddress(parseCidr("2001:db8::/32"))), "2001:db8::");
Sorting blocks by their first address
Sorting blocks by their first address
import { assertEquals } from "@std/assert"; import { cidrFirstAddress, parseCidr, stringifyCidr } from "@hertzg/ip/cidr"; const blocks = [ parseCidr("10.0.2.0/24"), parseCidr("10.0.0.0/24"), parseCidr("10.0.1.0/24"), ]; blocks.sort((a, b) => Number(cidrFirstAddress(a)) - Number(cidrFirstAddress(b))); assertEquals(blocks.map(stringifyCidr), [ "10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24", ]);
T extends Cidr
cidr: T
The CIDR block