Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ip-cidr

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ip-cidr - npm Package Compare versions

Comparing version 0.0.4 to 1.0.0

.npmignore

178

ip-cidr.js

@@ -7,101 +7,139 @@ "use strict";

class IPCIDR {
constructor(cidr) {
let ipAddressType = cidr.match(":")? ipAddress.Address6: ipAddress.Address4;
let address = new ipAddressType(cidr);
constructor(cidr) {
let ipAddressType = cidr.match(":") ? ipAddress.Address6 : ipAddress.Address4;
let address = new ipAddressType(cidr);
this._isValid = address.isValid();
this._isValid = address.isValid();
if(this._isValid) {
this.cidr = cidr;
this.ipAddressType = ipAddressType;
this.address = address;
this.addressStart = address.startAddress();
this.addressEnd = address.endAddress();
}
if (this._isValid) {
this.cidr = cidr;
this.ipAddressType = ipAddressType;
this.address = address;
this.addressStart = address.startAddress();
this.addressEnd = address.endAddress();
}
}
isValid() {
return this._isValid;
isValid() {
return this._isValid;
}
formatIP(address, options) {
options = options || {};
if (options.asBigInteger) {
return address.bigInteger();
}
else if (options.asAddressObject) {
return address;
}
formatIP(address, options) {
options = options || {};
return address.addressMinusSuffix;
}
if(options.asBigInteger) {
return address.bigInteger();
}
else if(options.asAddressObject) {
return address;
}
start(options) {
return this.formatIP(this.addressStart, options);
}
return address.addressMinusSuffix;
}
end(options) {
return this.formatIP(this.addressEnd, options);
}
start(options) {
return this.formatIP(this.addressStart, options);
}
toString() {
return this.cidr;
}
end(options) {
return this.formatIP(this.addressEnd, options);
}
toRange(options) {
return [this.formatIP(this.addressStart, options), this.formatIP(this.addressEnd, options)];
}
toString() {
return this.cidr;
toObject(options) {
return {
start: this.formatIP(this.addressStart, options),
end: this.formatIP(this.addressEnd, options)
};
}
toArray(options, results) {
options = options || {};
let list = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start).add(new BigInteger('1'));
let info = this.getChunkInfo(length, options);
if(results) {
Object.assign(results, info);
}
toRange(options) {
return [this.formatIP(this.addressStart, options), this.formatIP(this.addressEnd, options)];
for (let i = info.from; i < info.to; i++) {
let num = start.add(new BigInteger(i + ''));
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
list.push(ip);
}
toObject(options) {
return {
start: this.formatIP(this.addressStart, options),
end: this.formatIP(this.addressEnd, options)
};
return list;
}
arrayAction(fn, options, results) {
options = options || {};
let promise = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start).add(new BigInteger('1'));
let info = this.getChunkInfo(length, options);
if(results) {
Object.assign(results, info);
}
toArray(options) {
let list = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start);
for (let i = info.from; i < info.to; i++) {
let num = start.add(new BigInteger(i + ''));
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
options = options || {};
promise.push(fn(ip));
}
if(options.limit && length > options.limit) {
options.limitResult = {
maxLength: options.limit,
fullLength: parseInt(length)
};
return Promise.all(promise);
}
length = options.limit;
}
getChunkInfo(length, options) {
let from, limit, to;
for (let i = 0; i <= length; i++) {
let num = start.add(new BigInteger(i + ''));
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
if(options.from !== undefined) {
if(typeof options.from != 'object') {
from = new BigInteger(options.from + '');
}
}
else {
from = new BigInteger('0');
}
list.push(ip);
}
if(options.limit !== undefined) {
if(typeof options.limit != 'object') {
limit = new BigInteger(options.limit + '');
}
}
else {
limit = length;
}
return list;
if(limit.compareTo(length) > 0) {
limit = length.subtract(from);
}
arrayAction(fn, options) {
let promise = [];
let start = this.addressStart.bigInteger();
let end = this.addressEnd.bigInteger();
let length = end.subtract(start);
to = from.add(limit);
for (let i = 0; i <= length; i++) {
let num = start.add(new BigInteger(i + ''));
let ip = this.formatIP(this.ipAddressType.fromBigInteger(num), options);
promise.push(fn(ip));
}
return Promise.all(promise);
return {
from: from,
to: to,
limit: limit,
length: length
}
}
}
module.exports = IPCIDR;
{
"name": "ip-cidr",
"version": "0.0.4",
"description": "Module for work with CIDR (v4, v6)",
"main": "./ip-cidr.js",
"author": {
"name": "Balasyan Alexandr",
"email": "mywebstreet@gmail.com"
},
"keywords": [
"ip",
"cidr",
"range",
"from",
"to",
"array"
],
"license": "MIT",
"dependencies": {
"ip-address" : "^5",
"jsbn": "*"
},
"repository" : {
"type" : "git",
"url" : "https://github.com/ortexx/ip-cidr"
},
"engines": {
"node": ">=5.0.0"
}
"name": "ip-cidr",
"version": "1.0.0",
"description": "Module for work with CIDR (v4, v6)",
"main": "./ip-cidr.js",
"author": {
"name": "Balasyan Alexandr",
"email": "mywebstreet@gmail.com"
},
"keywords": [
"ip",
"cidr",
"range",
"from",
"to",
"array"
],
"license": "MIT",
"scripts": {
"test": "mocha"
},
"dependencies": {
"ip-address": "^5.8.6",
"jsbn": "^0.1.0"
},
"devDependencies": {
"mocha": "^3.2.0",
"chai": "^3.5.0"
},
"repository": {
"type": "git",
"url": "https://github.com/ortexx/ip-cidr"
},
"engines": {
"node": ">=5.0.0"
}
}

@@ -1,8 +0,8 @@

# Install
# Install
`npm install ip-cidr`
# About
Module for work with CIDR (v4, v6)
# About
Module for working with CIDR (v4, v6). Based on [ip-address](https://github.com/beaugunderson/ip-address)
# Example
# Example
```js

@@ -33,31 +33,34 @@ const CIDR = require("ip-cidr");

# FULL API
### .formatIP(address, options)
returns the "ip-address" module object ip address in the specified in options format
options are the same in all of the library functions and may include asBigInteger, asAddressObject or asString if options are empty
# FULL API
### .formatIP(address, options)
returns the "ip-address" module object ip address in the specified in options format
options are the same in all of the library functions and may include asBigInteger, asAddressObject or asString (by default)
### .start(options)
### .start(options)
get start ip
### .end(options)
### .end(options)
get end ip
### .toString()
### .toString()
get string cidr as "50.165.190.0/23"
### .toRange(options)
### .toRange(options)
get array of start and end ip [startIp, endIp]
### .toObject(options)
### .toObject(options)
get object of start and end ip {start: startIp, end: endIp}
### .toArray(options)
### .toArray(options, results)
get array of all ip in CIDR range
you can set maximum of array elements with options.limit
if the limit is reached, you will receive additional information in options.limitResult
you can get information by chunks using options.from and options.limit
this options can be integer or big integer("jsbn" instance)
you can send results argument(object) to get all chunk information inside
pagination starts from 0, all results values are instance of "jsbn"
### .arrayAction(fn, options)
run fn for every element of CIDR range
### .arrayAction(fn, options, results)
run fn for every element of CIDR range
you can use the same chunk options as in .toArray
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc