function cidrMerge
cidrMerge<T extends Cidr>(cidrs: readonly T[]): T[]

Merges CIDR blocks into the minimal covering set.

Dispatches to cidrv4Merge or cidrv6Merge based on the address type of the first element. All elements must be the same IP version. The result matches the dialect of the inputs, and is masked when they disagree (ADR 0006).

Examples

Merge IPv4 CIDRs

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

const result = cidrMerge([parseCidr("10.0.0.0/25"), parseCidr("10.0.0.128/25")]);
assertEquals(result.map((c) => stringifyCidr(c)), ["10.0.0.0/24"]);

Merge IPv6 CIDRs

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

const result = cidrMerge([parseCidr("2001:db8::/33"), parseCidr("2001:db8:8000::/33")]);
assertEquals(result.map((c) => stringifyCidr(c)), ["2001:db8::/32"]);

Empty array returns empty

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

assertEquals(cidrMerge([]), []);

Type Parameters

T extends Cidr

Parameters

cidrs: readonly T[]

The CIDR blocks to merge

Return Type

T[]

Minimal set of non-overlapping CIDR blocks, sorted by address