Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mirusresearch/node-ip
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanwayb committed Nov 18, 2015
2 parents f4d0ea6 + 9141089 commit 1f46f28
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ ip.subnet('192.168.1.134', '255.255.255.192')
// subnetMask: '255.255.255.192',
// subnetMaskLength: 26,
// numHosts: 62,
// length: 64 }
// length: 64,
// contains: function(addr){...} }
ip.cidrSubnet('192.168.1.134/26')
// Same as previous.

// range checking
ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true


// ipv4 long conversion
ip.toLong('127.0.0.1'); // 2130706433
ip.fromLong(2130706433); // '127.0.0.1'
Expand Down
5 changes: 4 additions & 1 deletion lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ ip.subnet = function subnet(addr, mask) {
subnetMaskLength: maskLength,
numHosts: numberOfAddresses <= 2 ?
numberOfAddresses : numberOfAddresses - 2,
length: numberOfAddresses
length: numberOfAddresses,
contains: function(other){
return networkAddress === ip.toLong(ip.mask(other, mask));
}
};
};

Expand Down
17 changes: 17 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ describe('IP library for node.js', function() {
it('should compute ipv4 subnet mask\'s length', function() {
assert.equal(ipv4Subnet.subnetMaskLength, 26);
});

it('should know whether a subnet contains an address', function(){
assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
});

it('should know whether a subnet does not contain an address', function(){
assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
});
});

describe('subnet() method with mask length 32', function() {
Expand Down Expand Up @@ -199,6 +207,15 @@ describe('IP library for node.js', function() {
it('should compute an ipv4 subnet mask\'s length', function() {
assert.equal(ipv4Subnet.subnetMaskLength, 26);
});

it('should know whether a subnet contains an address', function() {
assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
});

it('should know whether a subnet contains an address', function() {
assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
});

});

describe('cidr() method', function() {
Expand Down

0 comments on commit 1f46f28

Please sign in to comment.