cidr-regex 2.0.9 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +10 -13
  2. package/index.d.ts +55 -0
  3. package/index.js +12 -7
  4. package/package.json +11 -8
package/README.md CHANGED
@@ -1,36 +1,34 @@
1
1
  # cidr-regex
2
- [![](https://img.shields.io/npm/v/cidr-regex.svg?style=flat)](https://www.npmjs.org/package/cidr-regex) [![](https://img.shields.io/npm/dm/cidr-regex.svg)](https://www.npmjs.org/package/cidr-regex) [![](https://api.travis-ci.org/silverwind/cidr-regex.svg?style=flat)](https://travis-ci.org/silverwind/cidr-regex)
2
+ [![](https://img.shields.io/npm/v/cidr-regex.svg?style=flat)](https://www.npmjs.org/package/cidr-regex) [![](https://img.shields.io/npm/dm/cidr-regex.svg)](https://www.npmjs.org/package/cidr-regex)
3
3
 
4
4
  > Regular expression for matching IP addresses in CIDR notation
5
5
 
6
- ## Install
6
+ ## Usage
7
7
 
8
8
  ```sh
9
- $ npm install --save cidr-regex
9
+ $ npm i cidr-regex
10
10
  ```
11
11
 
12
- ## Usage
13
-
14
12
  ```js
15
- const cidrRegex = require('cidr-regex');
13
+ const cidrRegex = require("cidr-regex");
16
14
 
17
15
  // Contains a CIDR IP address?
18
- cidrRegex().test('foo 192.168.0.1/24');
16
+ cidrRegex().test("foo 192.168.0.1/24");
19
17
  //=> true
20
18
 
21
19
  // Is a CIDR IP address?
22
- cidrRegex({exact: true}).test('foo 192.168.0.1/24');
20
+ cidrRegex({exact: true}).test("foo 192.168.0.1/24");
23
21
  //=> false
24
22
 
25
- cidrRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8/64');
23
+ cidrRegex.v6({exact: true}).test("1:2:3:4:5:6:7:8/64");
26
24
  //=> true
27
25
 
28
- 'foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz'.match(cidrRegex());
29
- //=> ['192.168.0.1/24', '1:2:3:4:5:6:7:8/64']
26
+ // Extract CIDRs from string
27
+ "foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz".match(cidrRegex());
28
+ //=> ["192.168.0.1/24", "1:2:3:4:5:6:7:8/64"]
30
29
  ```
31
30
 
32
31
  ## API
33
-
34
32
  ### cidrRegex([options])
35
33
 
36
34
  Returns a regex for matching both IPv4 and IPv6 CIDR IP addresses.
@@ -50,7 +48,6 @@ Default: `false` *(Matches any CIDR IP address in a string)*
50
48
 
51
49
  Only match an exact string. Useful with `RegExp#test()` to check if a string is a CIDR IP address.
52
50
 
53
-
54
51
  ## Related
55
52
 
56
53
  - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation
package/index.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ declare namespace ip {
2
+ interface Options {
3
+ /**
4
+ Only match an exact string. Useful with `RegExp#test()` to check if a string is a CIDR IP address. *(`false` matches any CIDR IP address in a string)*
5
+
6
+ @default false
7
+ */
8
+ readonly exact?: boolean;
9
+ }
10
+ }
11
+
12
+ declare const ip: {
13
+ /**
14
+ Regular expression for matching IP addresses in CIDR notation.
15
+
16
+ @returns A regex for matching both IPv4 and IPv6 CIDR IP addresses.
17
+
18
+ @example
19
+ ```
20
+ import cidrRegex = require("cidr-regex");
21
+
22
+ // Contains a CIDR IP address?
23
+ cidrRegex().test("foo 192.168.0.1/24");
24
+ //=> true
25
+
26
+ // Is a CIDR IP address?
27
+ cidrRegex({exact: true}).test("foo 192.168.0.1/24");
28
+ //=> false
29
+
30
+ "foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz".match(cidrRegex());
31
+ //=> ["192.168.0.1/24", "1:2:3:4:5:6:7:8/64"]
32
+ ```
33
+ */
34
+ (options?: ip.Options): RegExp;
35
+
36
+ /**
37
+ @returns A regex for matching IPv4 CIDR IP addresses.
38
+ */
39
+ v4(options?: ip.Options): RegExp;
40
+
41
+ /**
42
+ @returns A regex for matching IPv6 CIDR IP addresses.
43
+
44
+ @example
45
+ ```
46
+ import cidrRegex = require("cidr-regex");
47
+
48
+ cidrRegex.v6({exact: true}).test("1:2:3:4:5:6:7:8/64");
49
+ //=> true
50
+ ```
51
+ */
52
+ v6(options?: ip.Options): RegExp;
53
+ };
54
+
55
+ export = ip;
package/index.js CHANGED
@@ -2,12 +2,17 @@
2
2
 
3
3
  const ipRegex = require("ip-regex");
4
4
 
5
- const v4 = ipRegex.v4().source + "\\/(3[0-2]|[12]?[0-9])";
6
- const v6 = ipRegex.v6().source + "\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])";
5
+ const defaultOpts = {exact: false};
7
6
 
8
- const ip = module.exports = opts => opts && opts.exact ?
9
- new RegExp(`(?:^${v4}$)|(?:^${v6}$)`) :
10
- new RegExp(`(?:${v4})|(?:${v6})`, "g");
7
+ const v4str = `${ipRegex.v4().source}\\/(3[0-2]|[12]?[0-9])`;
8
+ const v6str = `${ipRegex.v6().source}\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])`;
11
9
 
12
- ip.v4 = opts => opts && opts.exact ? new RegExp(`^${v4}$`) : new RegExp(v4, "g");
13
- ip.v6 = opts => opts && opts.exact ? new RegExp(`^${v6}$`) : new RegExp(v6, "g");
10
+ // can not precompile the non-exact regexes because global flag makes the regex object stateful
11
+ // which would require the user to reset .lastIndex on subsequent calls
12
+ const v4exact = new RegExp(`^${v4str}$`);
13
+ const v6exact = new RegExp(`^${v6str}$`);
14
+ const v46exact = new RegExp(`(?:^${v4str}$)|(?:^${v6str}$)`);
15
+
16
+ module.exports = ({exact} = defaultOpts) => exact ? v46exact : new RegExp(`(?:${v4str})|(?:${v6str})`, "g");
17
+ module.exports.v4 = ({exact} = defaultOpts) => exact ? v4exact : new RegExp(v4str, "g");
18
+ module.exports.v6 = ({exact} = defaultOpts) => exact ? v6exact : new RegExp(v6str, "g");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-regex",
3
- "version": "2.0.9",
3
+ "version": "3.1.1",
4
4
  "description": "Regular expression for matching IP addresses in CIDR notation",
5
5
  "author": "silverwind <me@silverwind.io>",
6
6
  "contributors": [
@@ -12,10 +12,11 @@
12
12
  "test": "make test"
13
13
  },
14
14
  "engines": {
15
- "node": ">=4"
15
+ "node": ">=10"
16
16
  },
17
17
  "files": [
18
- "index.js"
18
+ "index.js",
19
+ "index.d.ts"
19
20
  ],
20
21
  "keywords": [
21
22
  "cidr",
@@ -28,12 +29,14 @@
28
29
  "ip address"
29
30
  ],
30
31
  "dependencies": {
31
- "ip-regex": "^2.1.0"
32
+ "ip-regex": "^4.1.0"
32
33
  },
33
34
  "devDependencies": {
34
- "ava": "^0.25.0",
35
- "eslint": "^4.19.1",
36
- "eslint-config-silverwind": "^1.0.42",
37
- "updates": "^3.0.0"
35
+ "eslint": "7.8.1",
36
+ "eslint-config-silverwind": "18.0.8",
37
+ "jest": "26.4.2",
38
+ "tsd": "0.13.1",
39
+ "updates": "10.3.6",
40
+ "versions": "8.4.3"
38
41
  }
39
42
  }