Skip to content

Commit 1f46f28

Browse files
committed
2 parents f4d0ea6 + 9141089 commit 1f46f28

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ ip.subnet('192.168.1.134', '255.255.255.192')
5050
// subnetMask: '255.255.255.192',
5151
// subnetMaskLength: 26,
5252
// numHosts: 62,
53-
// length: 64 }
53+
// length: 64,
54+
// contains: function(addr){...} }
5455
ip.cidrSubnet('192.168.1.134/26')
5556
// Same as previous.
5657

58+
// range checking
59+
ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true
60+
61+
5762
// ipv4 long conversion
5863
ip.toLong('127.0.0.1'); // 2130706433
5964
ip.fromLong(2130706433); // '127.0.0.1'

lib/ip.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ ip.subnet = function subnet(addr, mask) {
205205
subnetMaskLength: maskLength,
206206
numHosts: numberOfAddresses <= 2 ?
207207
numberOfAddresses : numberOfAddresses - 2,
208-
length: numberOfAddresses
208+
length: numberOfAddresses,
209+
contains: function(other){
210+
return networkAddress === ip.toLong(ip.mask(other, mask));
211+
}
209212
};
210213
};
211214

test/api-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ describe('IP library for node.js', function() {
130130
it('should compute ipv4 subnet mask\'s length', function() {
131131
assert.equal(ipv4Subnet.subnetMaskLength, 26);
132132
});
133+
134+
it('should know whether a subnet contains an address', function(){
135+
assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
136+
});
137+
138+
it('should know whether a subnet does not contain an address', function(){
139+
assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
140+
});
133141
});
134142

135143
describe('subnet() method with mask length 32', function() {
@@ -199,6 +207,15 @@ describe('IP library for node.js', function() {
199207
it('should compute an ipv4 subnet mask\'s length', function() {
200208
assert.equal(ipv4Subnet.subnetMaskLength, 26);
201209
});
210+
211+
it('should know whether a subnet contains an address', function() {
212+
assert.equal(ipv4Subnet.contains('192.168.1.180'), true);
213+
});
214+
215+
it('should know whether a subnet contains an address', function() {
216+
assert.equal(ipv4Subnet.contains('192.168.1.195'), false);
217+
});
218+
202219
});
203220

204221
describe('cidr() method', function() {

0 commit comments

Comments
 (0)