function stringifyAddressv6
stringifyAddressv6(address: Addressv6 | ParsedAddressv6): string

Stringifies an IPv6 address to compressed colon-hexadecimal notation.

The output uses the canonical compressed form (RFC 5952):

  • Leading zeros in each group are omitted
  • The longest run of consecutive all-zero groups is replaced with ::
  • If there are multiple runs of the same length, the first one is compressed

Takes either the bare 128-bit bigint or a ParsedAddressv6; given the latter, a truthy zoneId is appended after %, so stringifyAddressv6(parseAddressv6(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, [fe80::1%25eth0]); 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 { stringifyAddressv6 } from "@hertzg/ip/addressv6";

assertEquals(stringifyAddressv6(0n), "::");
assertEquals(stringifyAddressv6(1n), "::1");
assertEquals(stringifyAddressv6(42540766411282592856903984951653826561n), "2001:db8::1");

Compression rules

import { assertEquals } from "@std/assert";
import { stringifyAddressv6 } from "@hertzg/ip/addressv6";

assertEquals(stringifyAddressv6(0x20010db8000000000000000000000001n), "2001:db8::1");
assertEquals(stringifyAddressv6(0x20010db800000000000000000000abcdn), "2001:db8::abcd");
assertEquals(stringifyAddressv6(0x00010000000000000001000000000001n), "1::1:0:0:1");

A parse result round-trips, zone included

import { assertEquals } from "@std/assert";
import { parseAddressv6, stringifyAddressv6 } from "@hertzg/ip/addressv6";

assertEquals(stringifyAddressv6(parseAddressv6("fe80::1%eth0")), "fe80::1%eth0");
assertEquals(stringifyAddressv6(parseAddressv6("2001:0db8:0000:0000:0000:0000:0000:0001")), "2001:db8::1");
assertEquals(stringifyAddressv6({ address: 1n, zoneId: "" }), "::1");

Error handling

import { assertThrows } from "@std/assert";
import { stringifyAddressv6 } from "@hertzg/ip/addressv6";

assertThrows(() => stringifyAddressv6(-1n), RangeError);
assertThrows(() => stringifyAddressv6(340282366920938463463374607431768211456n), RangeError);

Parameters

The address as a 128-bit bigint, or a parse result

Return Type

string

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

Throws

RangeError

If the address is negative or greater than 2^128-1