cidrVersion(cidr: string): IpVersion | undefined
Reports which IP version a CIDR notation string is written in.
The CIDR counterpart of addressVersion, with the same contract: 4,
6, or undefined, describing the string rather than the parsed value.
A plain address without a prefix length is not CIDR notation.
Both versions and the reject case
Both versions and the reject case
import { assertEquals } from "@std/assert"; import { cidrVersion } from "@hertzg/ip/version"; assertEquals(cidrVersion("10.0.0.0/8"), 4); assertEquals(cidrVersion("10.0.0.0/255.0.0.0"), 4); assertEquals(cidrVersion("2001:db8::/32"), 6); assertEquals(cidrVersion("fe80::%ether1/64"), 6); assertEquals(cidrVersion("10.0.0.0"), undefined); assertEquals(cidrVersion("10.0.0.0/33"), undefined); assertEquals(cidrVersion("garbage/24"), undefined);
IpVersion | undefined
4, 6, or undefined if the string is not CIDR notation