Skip to main content
Deno 2 is finally here 🎉️
Learn more

Useage

Deno

import * as netTools from "https://deno.land/x/cidr/mod.js"

API

import { isCIDRv4, isIP, isIPv4, isIPv4InCIDR } from "https://deno.land/x/cidr/mod.js"

// isIP returns `number`. It will return `0` if params isn't v4 or v6
isIP('::1') // returns 6
isIP('127.0.0.1') // returns 4
isIP('127.000.000.001') // returns 0

isCIDRv4("192.168.50.1/16") // true
isCIDRv4("192.168.50.1") // false

isIPv4InCIDR('192.168.50.1/16', '192.168.50.12') // true
isIPv4InCIDR('192.168.50.16/24', '192.168.50.255', { isBroadcast: true }) // true
isIPv4InCIDR('192.168.50.16/24', '192.168.50.0', { isNetwork: true }) // true

You can use isIPv4 if you only want to determine whether an IP is IPv4, it returns boolean.

  • isIPv4InCIDR is to determine whether an IP is within the CIDR range. The first parameter is CIDR range, the second paramter is IP address.

  • In the third paramters, you can choose isNetwork or Broadcast, which is used to judge whether is a network/broadcast address. But it’s better not to write both. And CIDR’s rang is preferably 0 ~ 30 (includes 0 and 30)

Sure, you can use CIDRv4IP if you need to iterate all IP in the CIDR range. Like this:

const gen = CIDRv4IP("1.1.1.1/30")
gen.next().value //"1.1.1.0"
gen.next().value //"1.1.1.1"
gen.next().value //"1.1.1.2"
gen.next().value //"1.1.1.3"