function stringifyCidrv6
stringifyCidrv6(cidr: Addressv6 | ParsedAddressv6 | ParsedCidrv6): string

Stringifies an IPv6 CIDR block, or an address, to CIDR notation with the address compressed.

The dialect is preserved: a PrefixedCidrv6 is written as address/prefixLength, a MaskedCidrv6 as address/mask with the mask in compressed colon-hexadecimal. The address is written as stored, host bits included. A bare Addressv6, or a ParsedAddressv6 with no prefix length or mask, gets the noun's default, /128. Nothing is mapped: an IPv4 number is a type error here, and mapFromAddressv4 is the deliberate step.

Given a ParsedCidrv6 (or parsed address), a truthy zoneId is written between the address and the /, so stringifyCidrv6(parseCidrv6(s)) gives back s for every accepted s in canonical form. zoneId must not contain %, / or whitespace: a zone containing any of them cannot be written in RFC 4007 textual form at all, because % is the delimiter, and the result would not re-parse. If you are producing a URI, percent-encode it there (RFC 9844); this package does not apply that transform, since %25 is also a valid interface index 25.

Examples

Basic stringifying

import { assertEquals } from "@std/assert";
import { parseCidrv6, stringifyCidrv6 } from "@hertzg/ip/cidrv6";

assertEquals(stringifyCidrv6(parseCidrv6("2001:db8::/32")), "2001:db8::/32");
assertEquals(stringifyCidrv6(parseCidrv6("fe80::%ether1/64")), "fe80::%ether1/64");

A masked block is written with its mask

import { assertEquals } from "@std/assert";
import { stringifyCidrv6 } from "@hertzg/ip/cidrv6";

assertEquals(
  stringifyCidrv6({ address: 0xfe80n << 112n, mask: 0xFFFFFFFF000000000000000000000000n }),
  "fe80::/ffff:ffff::",
);

A bare address is a /128, and a zone goes before the slash

import { assertEquals } from "@std/assert";
import { stringifyCidrv6 } from "@hertzg/ip/cidrv6";

assertEquals(stringifyCidrv6(1n), "::1/128");
assertEquals(
  stringifyCidrv6({ address: 0xfe800000000000000000000000000001n, zoneId: "eth0" }),
  "fe80::1%eth0/128",
);

Parameters

The CIDR block in either dialect, a parse result, or a bare address

Return Type

string

The CIDR notation string, e.g. "2001:db8::/32", "2001:db8::/ffff:ffff::", "fe80::%ether1/64"

Throws

RangeError

If the address, or a mask, is not a 128-bit unsigned integer