4.0.0 • Published 4 months ago

ip-cidr v4.0.0

Weekly downloads
39,757
License
MIT
Repository
github
Last release
4 months ago

Install

npm install ip-cidr

About

Module for working with CIDR (v4, v6). Based on ip-address. Since v4+ using javascript BigInt for big numbers handling.

Example

const IPCIDR = require("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

.formatIP(address, options)

to return an "ip-address" module object in the necessary format

.isValidAddress(address)

to check the address is valid or not (ip or cidr)

.isValidCIDR(address)

to check the address is valid (only cidr)

.createAddress(address)

to create an object address from the string

.prototype.contains(address)

to check the address belongs to the range

.prototype.start(options)

to get the start ip address

.prototype.end(options)

to get the end ip address

.prototype.toString()

to convert the cidr to a string like "50.165.190.0/23"

.prototype.toRange(options)

to convert the cidr to an array with start and end ip addresses startIp, endIp

.prototype.toObject(options)

to convert the cidr to an object with start and end ip addresses {start: startIp, end: endIp}

.prototype.toArray(options, results)

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

.prototype.loop(fn, options, results)

to run fn for each element of the range
you can use the same chunk options as in .toArray()

4.0.0

4 months ago

3.1.0

1 year ago

3.0.11

1 year ago

3.0.9

2 years ago

3.0.10

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

2.1.5

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

4 years ago

2.0.17

4 years ago

2.0.15

4 years ago

2.0.16

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.4

7 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago