matchSubnets

import { matchSubnets } from "jsr:@ende/net/unstable-ip";

function ยท experimental

function matchSubnets(addr: string, subnetOrIps: string[]): boolean

Checks if an IP address matches a subnet or specific IP address.

addr: string

The IP address to check (IPv4 or IPv6)

subnetOrIps: string[]

The subnet in CIDR notation (e.g., "192.168.1.0/24") or a specific IP address

Returns

true if the IP address matches the subnet or IP, false otherwise

Example: Check if the address is a IPv6

import { matchSubnets } from "@ende/net/unstable-ip"
import { assert, assertFalse } from "@ende/assert"

assert(matchSubnets("192.168.1.10", ["192.168.1.0/24"]));
assertFalse(matchSubnets("192.168.2.10", ["192.168.1.0/24"]));

assert(matchSubnets("2001:db8::ffff", ["2001:db8::/64"]));
assertFalse(matchSubnets("2001:db9::1", ["2001:db8::/64"]));

Source