Socket
Socket
Sign inDemoInstall

ip-subnet-calculator

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ip-subnet-calculator - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

CHANGES.md

235

lib/ip-subnet-calculator.js

@@ -1,3 +0,2 @@

/*jshint strict: false, bitwise: false */
/* global define,module */
'use strict';

@@ -7,10 +6,11 @@ /**

* @author Aleksi Asikainen
* @link https://github.com/franksrevenge/IPSubnetCalculator
*
* IpSubnetCalculator 0.1.0
* IpSubnetCalculator 1.1.0
*
* Copyright (c) 2013, Aleksi Asikainen
* Copyright (c) 2013-2016, Aleksi Asikainen
* All rights reserved.
*
* Released under Apache 2.0 License
* http://www.apache.org/licenses/LICENSE-2.0.html
* Released under MIT License
* https://opensource.org/licenses/MIT
*

@@ -42,4 +42,4 @@ *

*
* @param {string} ipStart Lowest IP in the range to be calculated in string format ("123.123.123.123")
* @param {string} ipEnd Highest IP (inclusive) in the range to be calculated in string format ("123.123.123.123")
* @param {string|number} ipStart Lowest IP in the range to be calculated
* @param {string|number} ipEnd Highest IP (inclusive) in the range to be calculated
*

@@ -72,15 +72,13 @@ * @return The function returns null in case of an error. Otherwise, an array containing one or more subnet

var rangeCollection = [];
if(
( ipStart === '' ) || ( ipStart === null ) || ( ipStart === false ) ||
( ipEnd === '' ) || ( ipEnd === null ) || ( ipEnd === false )
)
try
{
ipStartNum = this.toDecimal( ipStart );
ipEndNum = this.toDecimal( ipEnd );
}
catch( err )
{
return null;
}
ipStartNum = IpSubnetCalculator.toDecimal( ipStart );
ipEndNum = IpSubnetCalculator.toDecimal( ipEnd );
if( ipEndNum < ipStartNum )

@@ -95,3 +93,3 @@ {

{
var optimalRange = IpSubnetCalculator.getOptimalRange( ipCurNum, ipEndNum );
var optimalRange = this.getOptimalRange( ipCurNum, ipEndNum );

@@ -105,6 +103,6 @@ if( optimalRange === null )

ipCurNum = optimalRange.ipHigh + 1;
ipCurNum = optimalRange.ipHigh + 1;
}
return rangeCollection;
return rangeCollection;
},

@@ -116,4 +114,4 @@

*
* @param {string} ip IP address in string format
* @param {int} prefixSize Number of relevant bits in the subnet mask
* @param {string|number} ip IP address ("2.3.4.5")
* @param {int} prefixSize Number of relevant bits in the subnet mask (24)
* @return {object|null} Returns null in case of an error, and a subnet data object otherwise.

@@ -126,14 +124,14 @@ * For details about the subnet data object, see documentation of

{
if(
( ip === '' ) || ( ip === null ) || ( ip === false ) ||
( prefixSize === '' ) || ( prefixSize === null ) || ( prefixSize === false )
)
var ipNum;
try
{
ipNum = this.toDecimal( ip );
}
catch( err )
{
return null;
}
var ipNum = IpSubnetCalculator.toDecimal( ip );
return IpSubnetCalculator.getMaskRange( ipNum, prefixSize );
return this.getMaskRange( ipNum, prefixSize );
},

@@ -145,5 +143,5 @@

*
* @param {string} ip IP address in string format
* @param {string} subnetMask IP subnet mask in string format ("255.255.255.0")
* @return {object|null} Returns null in case of an error, and a subnet data object otherwise.
* @param {string|number} ip IP address ("2.3.4.5")
* @param {string|number} subnetMask IP subnet mask ("255.255.255.0")
* @return {object|null} Returns `null` in case of an error, and a subnet data object otherwise.
* For details about the subnet data object, see documentation of

@@ -155,18 +153,18 @@ * getMaskRange()

{
if(
( ip === '' ) || ( ip === null ) || ( ip === false ) ||
( subnetMask === '' ) || ( subnetMask === null ) || ( subnetMask === false )
)
var ipNum,
subnetMaskNum,
prefix = 0,
newPrefix = 0,
prefixSize;
try
{
ipNum = this.toDecimal( ip );
subnetMaskNum = this.toDecimal( subnetMask );
}
catch( err )
{
return null;
}
var ipNum = IpSubnetCalculator.toDecimal( ip );
var subnetMaskNum = IpSubnetCalculator.toDecimal( subnetMask );
var prefix = 0;
var newPrefix = 0;
var prefixSize;
for( prefixSize = 0; prefixSize < 32; prefixSize++ )

@@ -181,6 +179,6 @@ {

prefix = newPrefix;
}
prefix = newPrefix;
}
return IpSubnetCalculator.getMaskRange( ipNum, prefixSize );
return this.getMaskRange( ipNum, prefixSize );
},

@@ -193,5 +191,5 @@

*
* @param {int} ipNum IP start point
* @param {int} ipEndNum IP end point
* @return {object|null} Returns null on failure, otherwise an object with the following fields:
* @param {int} ipNum IP start point (inclusive)
* @param {int} ipEndNum IP end point (inclusive)
* @return {object|null} Returns `null` on failure, otherwise an object with the following fields:
*

@@ -212,8 +210,8 @@ * ipLow - Decimal representation of the lowest IP address in the subnet

{
var prefixSize;
var optimalRange = null;
var prefixSize,
optimalRange = null;
for( prefixSize = 32; prefixSize >= 0; prefixSize-- )
{
var maskRange = IpSubnetCalculator.getMaskRange( ipNum, prefixSize );
var maskRange = this.getMaskRange( ipNum, prefixSize );

@@ -231,3 +229,3 @@ if( ( maskRange.ipLow === ipNum ) && ( maskRange.ipHigh <= ipEndNum ) )

return optimalRange;
},
},

@@ -256,22 +254,21 @@

{
var prefixMask = IpSubnetCalculator.getPrefixMask( prefixSize );
var lowMask = IpSubnetCalculator.getMask( 32 - prefixSize );
var ipLow = ( ipNum & prefixMask ) >>> 0;
var ipHigh = ( ( ( ipNum & prefixMask ) >>> 0 ) + lowMask ) >>> 0;
var prefixMask = this.getPrefixMask( prefixSize ),
lowMask = this.getMask( 32 - prefixSize ),
ipLow = ( ipNum & prefixMask ) >>> 0,
ipHigh = ( ( ( ipNum & prefixMask ) >>> 0 ) + lowMask ) >>> 0;
return {
'ipLow' : ipLow,
'ipLowStr' : IpSubnetCalculator.toString( ipLow ),
ipLow : ipLow,
ipLowStr : this.toString( ipLow ),
'ipHigh' : ipHigh,
'ipHighStr' : IpSubnetCalculator.toString( ipHigh ),
ipHigh : ipHigh,
ipHighStr : this.toString( ipHigh ),
'prefixMask' : prefixMask,
'prefixMaskStr' : IpSubnetCalculator.toString( prefixMask ),
'prefixSize' : prefixSize,
prefixMask : prefixMask,
prefixMaskStr : this.toString( prefixMask ),
prefixSize : prefixSize,
'invertedMask' : lowMask,
'invertedMaskStr' : IpSubnetCalculator.toString( lowMask ),
'invertedSize' : 32 - prefixSize
invertedMask : lowMask,
invertedMaskStr : this.toString( lowMask ),
invertedSize : 32 - prefixSize
};

@@ -290,4 +287,4 @@ },

{
var mask = 0;
var i;
var mask = 0,
i;

@@ -312,4 +309,4 @@ for( i = 0; i < prefixSize; i++ )

{
var mask = 0;
var i;
var mask = 0,
i;

@@ -323,4 +320,55 @@ for( i = 0; i < maskSize; i++ )

},
/**
* Test whether string is an IP address
* @param {string} ip
* @returns {boolean}
* @public
*/
isIp : function( ip )
{
if( typeof ip !== 'string' )
{
return false;
}
var parts = ip.match( /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/ );
if( parts === null )
{
return false;
}
for( var i = 1; i <= 4; i++ )
{
var n = parseInt( parts[ i ], 10 );
if( ( n > 255 ) || ( n < 0 ) )
{
return false;
}
}
return true;
},
/**
* Test whether number is an IP address
* @param {number} ipNum
* @returns {boolean}
* @public
*/
isDecimalIp : function( ipNum )
{
return (
( typeof ipNum === 'number' ) && // is this a number?
( ipNum % 1 === 0 ) && // does the number have a decimal place?
( ipNum >= 0 ) &&
( ipNum <= 4294967295 )
);
},
/**

@@ -330,4 +378,5 @@ * Converts string formatted IPs to decimal representation

* @link http://javascript.about.com/library/blipconvert.htm
* @param {string} ipString IP address in string format
* @param {string|number} ipString IP address in string format. If a decimal representation given, it is returned unmodified.
* @return {int} Returns the IP address in decimal format
* @throws {Error} Throws an error, if `ipString` does not contain an IP address.
* @private

@@ -337,3 +386,14 @@ */

{
if( ( typeof ipString === 'number' ) && ( this.isDecimalIp( ipString ) === true ) )
{
return ipString;
}
if( this.isIp( ipString ) === false )
{
throw new Error( 'Not an IP address: ' + ipString );
}
var d = ipString.split( '.' );
return ( ( ( ( ( ( +d[ 0 ] ) * 256 ) + ( +d [ 1 ] ) ) * 256 ) + ( +d[ 2 ] ) ) * 256 ) + ( +d[ 3 ] );

@@ -347,14 +407,25 @@ },

* @link http://javascript.about.com/library/blipconvert.htm
* @param {int} ipNum IP address in decimal format
* @param {int} ipNum IP address in decimal format. If a string representation is given, it is returned unmodified.
* @return {string} Returns the IP address in string format
* @throws {Error} Throws an error, if `ipNum` is out of range, not a decimal, or not a number
* @private
*/
toString : function( ipNum )
{
{
if( ( typeof ipNum === 'string' ) && ( this.isIp( ipNum ) === true ) )
{
return ipNum;
}
if( this.isDecimalIp( ipNum ) === false )
{
throw new Error( 'Not a numeric IP address: ' + ipNum );
}
var d = ipNum % 256;
for( var i = 3; i > 0; i-- )
{
ipNum = Math.floor( ipNum / 256 );
d = ipNum % 256 + '.' + d;
{
ipNum = Math.floor( ipNum / 256 );
d = ipNum % 256 + '.' + d;
}

@@ -361,0 +432,0 @@

{
"author" : "Aleksi Asikainen",
"name" : "ip-subnet-calculator",
"description" : "Calculate optimal subnet masks for standard and non-standard IP ranges",
"homepage" : "https://github.com/franksrevenge/IPSubnetCalculator",
"version" : "1.0.3",
"main" : "lib/ip-subnet-calculator.js",
"keywords" : [ "ip", "subnet", "network", "netmask", "calculator" ],
"engines" : { "node" : "*" },
"dependencies" : {},
"devDependencies" : {},
"optionalDependencies" : {},
"license" : "MIT",
"repository" : {
"type" : "git",
"url" : "git://github.com/franksrevenge/IPSubnetCalculator.git"
}
"author": "Aleksi Asikainen",
"name": "ip-subnet-calculator",
"description": "Calculate optimal subnet masks for standard and non-standard IP ranges",
"homepage": "https://github.com/franksrevenge/IPSubnetCalculator",
"version": "1.1.0",
"files": [
"lib"
],
"main": "lib/ip-subnet-calculator.js",
"keywords": [
"ip",
"subnet",
"network",
"netmask",
"calculator",
"mask"
],
"engines": {
"node": "*"
},
"dependencies": {},
"optionalDependencies": {},
"license": "MIT",
"devDependencies": {
"istanbul": "^0.4.3",
"lodash": "4.12.x",
"mocha": "2.4.x",
"mocha-istanbul": "^0.2.0",
"should": "8.3.x"
},
"repository": {
"type": "git",
"url": "git://github.com/franksrevenge/IPSubnetCalculator.git"
},
"scripts": {
"test": "mocha"
}
}

@@ -43,3 +43,7 @@ IP Subnet Calculator

console.log( IpSubnetCalculator.isIp( '127.0.0.1' ) ); // "true"
console.log( IpSubnetCalculator.toDecimal( '127.0.0.1' ) ); // "2130706433"
console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );
```

@@ -56,3 +60,6 @@

{
console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );
console.log( IpSubnetCalculator.isIp( '127.0.0.1' ) ); // "true"
console.log( IpSubnetCalculator.toDecimal( '127.0.0.1' ) ); // "2130706433"
console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );
} );

@@ -66,5 +73,8 @@ ```

```html
<script src='IpSubnetCalculator.js'></script>
<script src='lib/ip-subnet-calculator.js'></script>
<script>
console.log( IpSubnetCalculator.isIp( '127.0.0.1' ) ); // "true"
console.log( IpSubnetCalculator.toDecimal( '127.0.0.1' ) ); // "2130706433"
console.log( IpSubnetCalculator.calculate( '5.4.3.21', '6.7.8.9' ) );

@@ -146,2 +156,21 @@ </script>

## Test Functions ##
### IpSubnetCalculator.isIp( ipStr ) ###
Tests whether string is an IP address.
*ipStr* (string) A string
The function returns a `true` if the string is an IP address, `false` otherwise.
### IpSubnetCalculator.isDecimalIp( ipNum ) ###
Tests whether string is an IP address.
*ipNum* (int) A number
The function returns a `true` if the number is an IP address, `false` otherwise.
## Conversion Functions ##

@@ -153,15 +182,20 @@

*ip* (string) IP address in string format
*ip* (string|number) IP address in string format
The function returns a decimal representation of an IP address as an integer.
The function returns a decimal representation of an IP address as an integer. If a valid numeric representation
of an IP is passed to this function, it is returned unmodified.
If an invalid value is passed to the function, it will `throw` an `Error` object.
### IpSubnetCalculator.toString( integer ) ###
*integer* (integer) Decimal representation of an IP address.
### IpSubnetCalculator.toString( num ) ###
The function returns an IP address as a string.
*num* (integer|string) Decimal representation of an IP address.
The function returns an IP address as a string. If a valid string representation of an IP is passed to this function,
it is returned unmodified.
If an invalid value is passed to the function, it will `throw` an `Error` object.
License

@@ -168,0 +202,0 @@ -------

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