function stringifyAddress
stringifyAddress(address: Address | ParsedAddress): string

Stringifies an IPv4 (number) or IPv6 (bigint) address, bare or parsed, to its standard notation.

Dispatches on typeof address: numbers are written as dotted decimal, bigints as compressed colon-hexadecimal. Given a ParsedAddress, a truthy zoneId is appended after %, so stringifyAddress(parseAddress(s)) gives back s for every accepted s in canonical form. zoneId must not contain %, / or whitespace; see stringifyAddressv6.

Since parseAddress unmaps IPv4-mapped IPv6 addresses to numbers, round-tripping a mapped address through parseAddress/stringifyAddress produces the IPv4 form (e.g. "192.168.1.1", not "::ffff:c0a8:101"). To produce the mapped IPv6 representation, use mapFromAddressv4 with stringifyAddressv6.

Examples

Example 1

import { assertEquals } from "@std/assert";
import { parseAddress, stringifyAddress } from "@hertzg/ip/address";

assertEquals(stringifyAddress(167772161), "10.0.0.1");
assertEquals(stringifyAddress(1n), "::1");
assertEquals(stringifyAddress(parseAddress("fe80::1%eth0")), "fe80::1%eth0");
assertEquals(stringifyAddress(parseAddress("192.168.1.1%ether1")), "192.168.1.1%ether1");

Producing the mapped IPv6 representation

import { assertEquals } from "@std/assert";
import { parseAddress, stringifyAddress } from "@hertzg/ip/address";
import { mapFromAddressv4, stringifyAddressv6 } from "@hertzg/ip/addressv6";

const { address } = parseAddress("::ffff:192.168.1.1");
assertEquals(stringifyAddress(address), "192.168.1.1");
assertEquals(stringifyAddressv6(mapFromAddressv4(address as number)), "::ffff:c0a8:101");

Parameters

The address as a number (IPv4) or bigint (IPv6), or a parse result

Return Type

string

The address string in dotted decimal or compressed colon-hexadecimal notation, %zoneId appended when there is one

Throws

RangeError

If the address is out of range

Usage

import { stringifyAddress } from "address.ts";