stringifyCidrv4(cidr: Addressv4 | ParsedAddressv4 | ParsedCidrv4): string
Stringifies an IPv4 CIDR block, or an address, to CIDR notation.
The dialect is preserved: a PrefixedCidrv4 is written as
address/prefixLength, a MaskedCidrv4 as address/mask with the
mask in dotted decimal. The address is written as stored, host bits
included. A bare Addressv4, or a ParsedAddressv4 with no
prefix length or mask, gets the noun's default, /32.
Given a ParsedCidrv4 (or parsed address), a truthy zoneId is
written between the address and the /, so
stringifyCidrv4(parseCidrv4(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.
Basic stringifying
Basic stringifying
import { assertEquals } from "@std/assert"; import { parseCidrv4, stringifyCidrv4 } from "@hertzg/ip/cidrv4"; assertEquals(stringifyCidrv4(parseCidrv4("192.168.1.0/24")), "192.168.1.0/24"); assertEquals(stringifyCidrv4({ address: 3232235777, prefixLength: 24 }), "192.168.1.1/24");
A masked block is written with its mask
A masked block is written with its mask
import { assertEquals } from "@std/assert"; import { stringifyCidrv4 } from "@hertzg/ip/cidrv4"; assertEquals( stringifyCidrv4({ address: 167772160, mask: 0xFF000000 }), "10.0.0.0/255.0.0.0", );
A bare address is a /32, and a zone goes before the slash
A bare address is a /32, and a zone goes before the slash
import { assertEquals } from "@std/assert"; import { stringifyCidrv4 } from "@hertzg/ip/cidrv4"; assertEquals(stringifyCidrv4(3232235777), "192.168.1.1/32"); assertEquals(stringifyCidrv4({ address: 3232235777, zoneId: "ether1" }), "192.168.1.1%ether1/32"); assertEquals( stringifyCidrv4({ address: 3232235777, zoneId: "ether1", prefixLength: 24 }), "192.168.1.1%ether1/24", );
cidr: Addressv4 | ParsedAddressv4 | ParsedCidrv4
The CIDR block in either dialect, a parse result, or a bare address