rangeToCidr() function

Covers an address range with the fewest CIDR records that fit it exactly.

Signature:

export declare function rangeToCidr(start: string, end: string, type?: NetworkType, canonical?: boolean): string[];

Parameters

Parameter

Type

Description

start

string

first address in the range, inclusive

end

string

last address in the range, inclusive

type

NetworkType

(Optional) the family, if known; omit to have it detected from start

canonical

boolean

(Optional) for IPv6, render expanded rather than compressed addresses

Returns:

string[]

The records covering exactly [start, end].

Exceptions

TypeError if either address is invalid, or if type disagrees with start. Note that only start is checked against type, so a mismatched end is converted as whatever family start established.

Default Value

canonical defaults to false

Remarks

intRangeToCidr() with both ends converted by ipToInt() first — the same minimal-cover behaviour, taking text instead of integers.

Example

rangeToCidr('10.0.0.0', '10.0.0.255');  // ['10.0.0.0/24']
rangeToCidr('10.0.0.1', '10.0.0.2');    // ['10.0.0.1/32', '10.0.0.2/32']

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.