parseAddress(address: string,options?: ParseOptions): ParsedAddress
Parses an IPv4 or IPv6 address string, with an optional zone ID, to its numeric value.
Detects the IP version from the address slot -- a : means IPv6
(bigint), otherwise IPv4 (number) -- and hands the string to
parseAddressv6 or parseAddressv4, so the accepted grammar
is exactly theirs: RFC 4291 / dotted decimal, an optional %zoneId
carried verbatim, no prefix.
IPv4-mapped IPv6 addresses (::ffff:a.b.c.d) are unmapped to their IPv4
number by default (ADR 0004); pass { unmapToV4: false } to keep the
bigint, or use parseAddressv6, which never unmaps. The zone ID,
if any, is carried either way.
Both versions
Both versions
import { assertEquals } from "@std/assert"; import { parseAddress } from "@hertzg/ip/address"; assertEquals(parseAddress("10.0.0.1"), { address: 167772161 }); assertEquals(parseAddress("::1"), { address: 1n }); assertEquals(parseAddress("192.168.1.1%ether1"), { address: 3232235777, zoneId: "ether1" }); assertEquals(parseAddress("fe80::1%eth0"), { address: 0xfe800000000000000000000000000001n, zoneId: "eth0" });
IPv4-mapped addresses unmap by default
IPv4-mapped addresses unmap by default
import { assertEquals } from "@std/assert"; import { parseAddress } from "@hertzg/ip/address"; assertEquals(parseAddress("::ffff:192.168.1.1"), { address: 3232235777 }); assertEquals(parseAddress("::ffff:192.168.1.1", { unmapToV4: false }), { address: 0xffffc0a80101n }); assertEquals(parseAddress("::ffff:192.168.1.1%eth0"), { address: 3232235777, zoneId: "eth0" });
The address string in dotted decimal or colon-hexadecimal notation, with an optional %zoneId
options: ParseOptions
unmapToV4, default true
The parsed address as number (IPv4) or bigint (IPv6), and the zone ID if there was one