From 2be8b1522fad38c538d56b797b49a4c0afd38f25 Mon Sep 17 00:00:00 2001 From: Don Spaulding Date: Sat, 6 Dec 2014 14:39:19 -0600 Subject: [PATCH 1/4] Describe desired API for ip.subnet().contains() --- test/api-test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/api-test.js b/test/api-test.js index 20295f8..31dcdad 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -114,6 +114,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() { @@ -183,6 +191,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 does not contain an address', function(){ + assert.equal(ipv4Subnet.contains('192.168.1.195'), false); + }); + }); describe('cidr() method', function() { From bed07129f43e1404ed037d6c239ead1e34459ab8 Mon Sep 17 00:00:00 2001 From: Don Spaulding II Date: Sat, 6 Dec 2014 14:51:21 -0600 Subject: [PATCH 2/4] Implement contains functionality. --- lib/ip.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ip.js b/lib/ip.js index d8ecd7d..6cb6b90 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -171,7 +171,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)); + } }; } From d667780bb03c6dce46cca5d70a56d479f00fb0b4 Mon Sep 17 00:00:00 2001 From: Don Spaulding II Date: Sat, 6 Dec 2014 14:54:11 -0600 Subject: [PATCH 3/4] Show usage for contains() --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 40dac31..808c1cd 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,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' From 9141089c48d4f1d6e0a3cef2b811041bbbbfe249 Mon Sep 17 00:00:00 2001 From: Don Spaulding II Date: Wed, 10 Dec 2014 13:56:45 -0600 Subject: [PATCH 4/4] Style conformance. --- lib/ip.js | 2 +- test/api-test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ip.js b/lib/ip.js index 6cb6b90..a4bf0c8 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -173,7 +173,7 @@ ip.subnet = function subnet(addr, mask) { numberOfAddresses : numberOfAddresses - 2, length: numberOfAddresses, contains: function(other){ - return networkAddress === ip.toLong(ip.mask(other, mask)); + return networkAddress === ip.toLong(ip.mask(other, mask)); } }; } diff --git a/test/api-test.js b/test/api-test.js index 31dcdad..7bec936 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -192,11 +192,11 @@ describe('IP library for node.js', function() { assert.equal(ipv4Subnet.subnetMaskLength, 26); }); - it('should know whether a subnet contains an address', function(){ + 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(){ + it('should know whether a subnet contains an address', function() { assert.equal(ipv4Subnet.contains('192.168.1.195'), false); });