cidr-regex 3.1.0 → 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 (2) hide show
  1. package/index.js +12 -7
  2. package/package.json +1 -1
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 cidr = 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
- cidr.v4 = opts => opts && opts.exact ? new RegExp(`^${v4}$`) : new RegExp(v4, "g");
13
- cidr.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": "3.1.0",
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": [