diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2020-09-30 20:26:31 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2020-09-30 20:26:31 +0300 |
commit | a27e8d00664e5d4c3e81d664253a10176adb39c8 (patch) | |
tree | a028261d73d4e99b649edea99b85306b08b3d07b /node_modules/cidr-regex | |
parent | 2aa9a1f8a5773b9a960b14b51c8111fb964bc9ae (diff) |
is-cidr@4.0.2
Diffstat (limited to 'node_modules/cidr-regex')
-rw-r--r-- | node_modules/cidr-regex/README.md | 23 | ||||
-rw-r--r-- | node_modules/cidr-regex/index.d.ts | 55 | ||||
-rw-r--r-- | node_modules/cidr-regex/index.js | 19 | ||||
-rw-r--r-- | node_modules/cidr-regex/package.json | 19 |
4 files changed, 88 insertions, 28 deletions
diff --git a/node_modules/cidr-regex/README.md b/node_modules/cidr-regex/README.md index d24d7071a..b2d110242 100644 --- a/node_modules/cidr-regex/README.md +++ b/node_modules/cidr-regex/README.md @@ -1,36 +1,34 @@ # cidr-regex -[![](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) +[![](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) > Regular expression for matching IP addresses in CIDR notation -## Install +## Usage ```sh -$ npm install --save cidr-regex +$ npm i cidr-regex ``` -## Usage - ```js -const cidrRegex = require('cidr-regex'); +const cidrRegex = require("cidr-regex"); // Contains a CIDR IP address? -cidrRegex().test('foo 192.168.0.1/24'); +cidrRegex().test("foo 192.168.0.1/24"); //=> true // Is a CIDR IP address? -cidrRegex({exact: true}).test('foo 192.168.0.1/24'); +cidrRegex({exact: true}).test("foo 192.168.0.1/24"); //=> false -cidrRegex.v6({exact: true}).test('1:2:3:4:5:6:7:8/64'); +cidrRegex.v6({exact: true}).test("1:2:3:4:5:6:7:8/64"); //=> true -'foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz'.match(cidrRegex()); -//=> ['192.168.0.1/24', '1:2:3:4:5:6:7:8/64'] +// Extract CIDRs from string +"foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz".match(cidrRegex()); +//=> ["192.168.0.1/24", "1:2:3:4:5:6:7:8/64"] ``` ## API - ### cidrRegex([options]) 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)* Only match an exact string. Useful with `RegExp#test()` to check if a string is a CIDR IP address. - ## Related - [is-cidr](https://github.com/silverwind/is-cidr) - Check if a string is an IP address in CIDR notation diff --git a/node_modules/cidr-regex/index.d.ts b/node_modules/cidr-regex/index.d.ts new file mode 100644 index 000000000..9099caecc --- /dev/null +++ b/node_modules/cidr-regex/index.d.ts @@ -0,0 +1,55 @@ +declare namespace ip { + interface Options { + /** + 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)* + + @default false + */ + readonly exact?: boolean; + } +} + +declare const ip: { + /** + Regular expression for matching IP addresses in CIDR notation. + + @returns A regex for matching both IPv4 and IPv6 CIDR IP addresses. + + @example + ``` + import cidrRegex = require("cidr-regex"); + + // Contains a CIDR IP address? + cidrRegex().test("foo 192.168.0.1/24"); + //=> true + + // Is a CIDR IP address? + cidrRegex({exact: true}).test("foo 192.168.0.1/24"); + //=> false + + "foo 192.168.0.1/24 bar 1:2:3:4:5:6:7:8/64 baz".match(cidrRegex()); + //=> ["192.168.0.1/24", "1:2:3:4:5:6:7:8/64"] + ``` + */ + (options?: ip.Options): RegExp; + + /** + @returns A regex for matching IPv4 CIDR IP addresses. + */ + v4(options?: ip.Options): RegExp; + + /** + @returns A regex for matching IPv6 CIDR IP addresses. + + @example + ``` + import cidrRegex = require("cidr-regex"); + + cidrRegex.v6({exact: true}).test("1:2:3:4:5:6:7:8/64"); + //=> true + ``` + */ + v6(options?: ip.Options): RegExp; +}; + +export = ip; diff --git a/node_modules/cidr-regex/index.js b/node_modules/cidr-regex/index.js index f141ce14a..0f12ee10b 100644 --- a/node_modules/cidr-regex/index.js +++ b/node_modules/cidr-regex/index.js @@ -2,12 +2,17 @@ const ipRegex = require("ip-regex"); -const v4 = ipRegex.v4().source + "\\/(3[0-2]|[12]?[0-9])"; -const v6 = ipRegex.v6().source + "\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])"; +const defaultOpts = {exact: false}; -const cidr = module.exports = opts => opts && opts.exact ? - new RegExp(`(?:^${v4}$)|(?:^${v6}$)`) : - new RegExp(`(?:${v4})|(?:${v6})`, "g"); +const v4str = `${ipRegex.v4().source}\\/(3[0-2]|[12]?[0-9])`; +const v6str = `${ipRegex.v6().source}\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])`; -cidr.v4 = opts => opts && opts.exact ? new RegExp(`^${v4}$`) : new RegExp(v4, "g"); -cidr.v6 = opts => opts && opts.exact ? new RegExp(`^${v6}$`) : new RegExp(v6, "g"); +// can not precompile the non-exact regexes because global flag makes the regex object stateful +// which would require the user to reset .lastIndex on subsequent calls +const v4exact = new RegExp(`^${v4str}$`); +const v6exact = new RegExp(`^${v6str}$`); +const v46exact = new RegExp(`(?:^${v4str}$)|(?:^${v6str}$)`); + +module.exports = ({exact} = defaultOpts) => exact ? v46exact : new RegExp(`(?:${v4str})|(?:${v6str})`, "g"); +module.exports.v4 = ({exact} = defaultOpts) => exact ? v4exact : new RegExp(v4str, "g"); +module.exports.v6 = ({exact} = defaultOpts) => exact ? v6exact : new RegExp(v6str, "g"); diff --git a/node_modules/cidr-regex/package.json b/node_modules/cidr-regex/package.json index 7e5cdf7e3..8ddef4ed9 100644 --- a/node_modules/cidr-regex/package.json +++ b/node_modules/cidr-regex/package.json @@ -1,6 +1,6 @@ { "name": "cidr-regex", - "version": "2.0.10", + "version": "3.1.1", "description": "Regular expression for matching IP addresses in CIDR notation", "author": "silverwind <me@silverwind.io>", "contributors": [ @@ -12,10 +12,11 @@ "test": "make test" }, "engines": { - "node": ">=4" + "node": ">=10" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "cidr", @@ -28,12 +29,14 @@ "ip address" ], "dependencies": { - "ip-regex": "^2.1.0" + "ip-regex": "^4.1.0" }, "devDependencies": { - "eslint": "^5.6.0", - "eslint-config-silverwind": "^2.0.9", - "updates": "^4.3.0", - "ver": "^2.0.1" + "eslint": "7.8.1", + "eslint-config-silverwind": "18.0.8", + "jest": "26.4.2", + "tsd": "0.13.1", + "updates": "10.3.6", + "versions": "8.4.3" } } |