File tree 3 files changed +27
-2
lines changed
3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -50,10 +50,15 @@ ip.subnet('192.168.1.134', '255.255.255.192')
50
50
// subnetMask: '255.255.255.192',
51
51
// subnetMaskLength: 26,
52
52
// numHosts: 62,
53
- // length: 64 }
53
+ // length: 64,
54
+ // contains: function(addr){...} }
54
55
ip .cidrSubnet (' 192.168.1.134/26' )
55
56
// Same as previous.
56
57
58
+ // range checking
59
+ ip .cidrSubnet (' 192.168.1.134/26' ).contains (' 192.168.1.190' ) // true
60
+
61
+
57
62
// ipv4 long conversion
58
63
ip .toLong (' 127.0.0.1' ); // 2130706433
59
64
ip .fromLong (2130706433 ); // '127.0.0.1'
Original file line number Diff line number Diff line change @@ -205,7 +205,10 @@ ip.subnet = function subnet(addr, mask) {
205
205
subnetMaskLength : maskLength ,
206
206
numHosts : numberOfAddresses <= 2 ?
207
207
numberOfAddresses : numberOfAddresses - 2 ,
208
- length : numberOfAddresses
208
+ length : numberOfAddresses ,
209
+ contains : function ( other ) {
210
+ return networkAddress === ip . toLong ( ip . mask ( other , mask ) ) ;
211
+ }
209
212
} ;
210
213
} ;
211
214
Original file line number Diff line number Diff line change @@ -130,6 +130,14 @@ describe('IP library for node.js', function() {
130
130
it ( 'should compute ipv4 subnet mask\'s length' , function ( ) {
131
131
assert . equal ( ipv4Subnet . subnetMaskLength , 26 ) ;
132
132
} ) ;
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
+ } ) ;
133
141
} ) ;
134
142
135
143
describe ( 'subnet() method with mask length 32' , function ( ) {
@@ -199,6 +207,15 @@ describe('IP library for node.js', function() {
199
207
it ( 'should compute an ipv4 subnet mask\'s length' , function ( ) {
200
208
assert . equal ( ipv4Subnet . subnetMaskLength , 26 ) ;
201
209
} ) ;
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
+
202
219
} ) ;
203
220
204
221
describe ( 'cidr() method' , function ( ) {
You can’t perform that action at this time.
0 commit comments