Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added list support

  • Loading branch information...
commit 7590d2ed7eee5dc9acc9572d2bdeb127fe9b9382 1 parent 003ae9f
keverw authored

Showing 3 changed files with 38 additions and 12 deletions. Show diff stats Hide diff stats

  1. +27 11 range_check.js
  2. +8 0 readme.md
  3. +3 1 tests/v4_range.js
38 range_check.js
@@ -21,18 +21,34 @@
21 21
22 22 function in_range(addr, range)
23 23 {
24   - if (range.indexOf('/') !== -1)
  24 + if (typeof(range) == 'string')
25 25 {
26   - var range_data = range.split('/');
27   -
28   - var ipaddr = require('ipaddr.js');
29   -
30   - var parse_addr = ipaddr.parse(addr);
31   - var range = ipaddr.parse(range_data[0]);
32   -
33   - console.log(parse_addr);
34   -
35   - return parse_addr.match(range, range_data[1]);
  26 + if (range.indexOf('/') !== -1)
  27 + {
  28 + var range_data = range.split('/');
  29 +
  30 + var ipaddr = require('ipaddr.js');
  31 +
  32 + var parse_addr = ipaddr.parse(addr);
  33 + var range = ipaddr.parse(range_data[0]);
  34 +
  35 + return parse_addr.match(range, range_data[1]);
  36 + }
  37 + else
  38 + {
  39 + return false;
  40 + }
  41 + }
  42 + else if (typeof(range) == 'object')//list
  43 + {
  44 + for(var check_range in range)
  45 + {
  46 + if (in_range(addr, range[check_range]) == true)
  47 + {
  48 + return true;
  49 + }
  50 + }
  51 + return false;
36 52 }
37 53 else
38 54 {
8 readme.md
Source Rendered
@@ -26,4 +26,12 @@ console.log(range_check.in_range('10.0.1.5', '10.0.0.0/8')); //returns true
26 26 console.log(range_check.in_range('192.0.1.5', '10.0.0.0/8')); //returns false
27 27
28 28 console.log(range_check.in_range('2001:db8:1234::1', '2001:db8::/32')); //returns true
  29 +```
  30 +
  31 +You can also give a list of ranges
  32 +
  33 +```
  34 +var range_check = require('range_check');
  35 +console.log(range_check.in_range('192.168.1.1', ['10.0.0.0/8', '192.0.0.0/8'])); //returns true
  36 +
29 37 ```
4 tests/v4_range.js
@@ -4,4 +4,6 @@ console.log(range_check.in_range('10.0.1.5', '10.0.0.0/8'));
4 4
5 5 //console.log(range_check.in_range('192.0.1.5', '10.0.0.0/8'));
6 6
7   -//console.log(range_check.in_range('2001:db8:1234::1', '2001:db8::/32'));
  7 +//console.log(range_check.in_range('2001:db8:1234::1', '2001:db8::/32'));
  8 +
  9 +console.log(range_check.in_range('192.168.1.1', ['10.0.0.0/8', '192.0.0.0/8']));

0 comments on commit 7590d2e

Please sign in to comment.
Something went wrong with that request. Please try again.