function stringifyCidr
stringifyCidr(cidr: Address | ParsedAddress | ParsedCidr): string

Stringifies a CIDR block of either version, or an address, to CIDR notation.

Dispatches on the version of the address and hands the value to stringifyCidrv4 or stringifyCidrv6, so their rules apply: the dialect is preserved (/N for a prefixed block, /mask in the address notation of its version for a masked one), a bare address gets the noun's default (/32 or /128), and a truthy zoneId is written between the address and the /, so stringifyCidr(parseCidr(s)) gives back s for every accepted s in canonical form. zoneId must not contain %, / or whitespace; see stringifyCidrv6.

Examples

Example 1

import { assertEquals } from "@std/assert";
import { parseCidr, stringifyCidr } from "@hertzg/ip/cidr";

assertEquals(stringifyCidr(parseCidr("192.168.1.0/24")), "192.168.1.0/24");
assertEquals(stringifyCidr(parseCidr("2001:db8::/32")), "2001:db8::/32");
assertEquals(stringifyCidr(parseCidr("fe80::%ether1/64")), "fe80::%ether1/64");

Masked blocks are written with their mask

import { assertEquals } from "@std/assert";
import { stringifyCidr } from "@hertzg/ip/cidr";

assertEquals(
  stringifyCidr({ address: 167772160, mask: 0xFF000000 }),
  "10.0.0.0/255.0.0.0",
);
assertEquals(
  stringifyCidr({ address: 0xfe80n << 112n, mask: 0xFFFFFFFF000000000000000000000000n }),
  "fe80::/ffff:ffff::",
);

A bare address gets the noun's default

import { assertEquals } from "@std/assert";
import { stringifyCidr } from "@hertzg/ip/cidr";

assertEquals(stringifyCidr(3232235777), "192.168.1.1/32");
assertEquals(stringifyCidr(0xfe800000000000000000000000000001n), "fe80::1/128");
assertEquals(
  stringifyCidr({ address: 0xfe800000000000000000000000000001n, zoneId: "eth0" }),
  "fe80::1%eth0/128",
);

Parameters

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

Return Type

string

The CIDR notation string

Throws

RangeError

If the address, or a mask, is out of range for its version