Install
npm install ip-cidrAbout
Module for working with CIDR (v4, v6). Based on
ip-address.
Since v4+ using javascript BigInt for big numbers handling.
Example
``js
import IPCIDR from 'ip-cidr';
const address = "50.165.190.0/23";
if(!IPCIDR.isValidCIDR(address)) {
return;
}
const cidr = new IPCIDR(address);
// get start ip address as a string
cidr.start();
// get end ip address as a big integer
cidr.end({ type: "bigInteger" });
// do something with each element of the range
cidr.loop(ip => console.log(ip), { type: "addressObject" });
// get an array of all ip addresses in the range as a big integer;
cidr.toArray({ type: "bigInteger" });
// get an array by chunks using from/limit
cidr.toArray({ from: 1, limit: 2n });
// get an array by chunks using from/to
cidr.toArray({ from: BigInt('1'), to: 3 });
cidr.toArray({ from: '50.165.190.1', to: '50.165.190.3' });
// get an array of start and end ip addresses as a string [startIpAsString, endIpAsString]
cidr.toRange();
``
Client side
Load __/dist/ip-cidr.js__ as a script and you can get the library in __window.IPCIDR__
API
$3
to return an "ip-address" module object in the necessary format
$3
to check the address is valid or not (ip or cidr)
$3
to check the address is valid (only cidr)
$3
to create an object address from the string
$3
to check the address belongs to the range
$3
to get the start ip address
$3
to get the end ip address
$3
to convert the cidr to a string like "50.165.190.0/23"
$3
to convert the cidr to an array with start and end ip addresses [startIp, endIp]
$3
to convert the cidr to an object with start and end ip addresses {start: startIp, end: endIp}
$3
to convert the cidr to an array with all ip addresses in the range
you can get information by chunks using
options.from/options.limit or
options.from/options.to you can pass the second argument "results" (object) to get all chunk pagination information
$3
to run __fn__ for each element of the range
you can use the same chunk options as in __.toArray()__