cidr-tools 3.0.6 → 4.0.0

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.
package/README.md CHANGED
@@ -50,6 +50,13 @@ Returns an array of individual IPs contained in the networks.
50
50
 
51
51
  Returns a boolean that indicates if `networksA` overlap (intersect) with `networksB`.
52
52
 
53
+ ### cidrTools.contains(networkA, networkB)
54
+
55
+ - `networkA` *String*: A CIDR or IP address.
56
+ - `networkB` *String*: A CIDR or IP address.
57
+
58
+ Returns a boolean that indicates whether `networksA` fully contains `networksB`.
59
+
53
60
  ### cidrTools.normalize(network)
54
61
 
55
62
  - `network` *String*: A CIDR or IP address.
package/index.d.ts CHANGED
@@ -13,6 +13,7 @@ interface CIDRTools {
13
13
  expand(networks: Networks): Network[];
14
14
  overlap(networksA: Networks, networksB: Networks): boolean;
15
15
  normalize(cidr: Network): Network;
16
+ contains(networkA: Network, networkB: Network): boolean;
16
17
  }
17
18
 
18
19
  declare const cidrTools : CIDRTools;
package/index.js CHANGED
@@ -61,11 +61,18 @@ function uniq(arr) {
61
61
  return [...new Set(arr)];
62
62
  }
63
63
 
64
- function doNetsOverlap(a, b) {
64
+ // utility function that returns boundaries of two networks
65
+ function getBoundaries(a, b) {
65
66
  const aStart = a.start({type: "bigInteger"});
66
67
  const bStart = b.start({type: "bigInteger"});
67
68
  const aEnd = a.end({type: "bigInteger"});
68
69
  const bEnd = b.end({type: "bigInteger"});
70
+ return {aStart, bStart, aEnd, bEnd};
71
+ }
72
+
73
+ // returns whether networks fully or partially overlap
74
+ function doNetsOverlap(a, b) {
75
+ const {aStart, bStart, aEnd, bEnd} = getBoundaries(a, b);
69
76
 
70
77
  // aaa
71
78
  // bbb
@@ -78,12 +85,24 @@ function doNetsOverlap(a, b) {
78
85
  return true;
79
86
  }
80
87
 
88
+ // returns whether network a fully contains network b;
89
+ function contains(a, b) {
90
+ const {aStart, bStart, aEnd, bEnd} = getBoundaries(a, b);
91
+
92
+ // aaa
93
+ // bbbb
94
+ if (bStart.compareTo(aStart) < 0) return false; // a starts after b
95
+
96
+ // aaa
97
+ // bbbb
98
+ if (bEnd.compareTo(aEnd) > 0) return false; // b starts after a
99
+
100
+ return true;
101
+ }
102
+
81
103
  // exclude b from a and return remainder cidrs
82
104
  function excludeNets(a, b, v) {
83
- const aStart = a.start({type: "bigInteger"});
84
- const bStart = b.start({type: "bigInteger"});
85
- const aEnd = a.end({type: "bigInteger"});
86
- const bEnd = b.end({type: "bigInteger"});
105
+ const {aStart, bStart, aEnd, bEnd} = getBoundaries(a, b);
87
106
  const parts = [];
88
107
 
89
108
  // compareTo returns negative if left is less than right
@@ -341,6 +360,7 @@ module.exports.overlap = (a, b) => {
341
360
  for (const b of bNets) {
342
361
  const bParsed = parse(b);
343
362
 
363
+ // version mismatch
344
364
  if (aParsed.address.v4 !== bParsed.address.v4) {
345
365
  continue;
346
366
  }
@@ -353,3 +373,15 @@ module.exports.overlap = (a, b) => {
353
373
 
354
374
  return false;
355
375
  };
376
+
377
+ module.exports.contains = (a, b) => {
378
+ const aParsed = parse(a);
379
+ const bParsed = parse(b);
380
+
381
+ // version mismatch
382
+ if (aParsed.address.v4 !== bParsed.address.v4) {
383
+ return false;
384
+ }
385
+
386
+ return contains(aParsed, bParsed);
387
+ };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "3.0.6",
3
+ "version": "4.0.0",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR network lists",
6
6
  "repository": "silverwind/cidr-tools",
7
7
  "license": "BSD-2-Clause",
8
8
  "engines": {
9
- "node": ">=10"
9
+ "node": ">=12.17.0"
10
10
  },
11
11
  "keywords": [
12
12
  "cidr",
@@ -35,14 +35,10 @@
35
35
  "string-natural-compare": "^3.0.1"
36
36
  },
37
37
  "devDependencies": {
38
- "eslint": "8.4.1",
39
- "eslint-config-silverwind": "47.1.0",
40
- "jest": "27.4.5",
41
- "updates": "12.2.3",
42
- "versions": "9.1.1"
43
- },
44
- "jest": {
45
- "verbose": false,
46
- "testTimeout": 30000
38
+ "eslint": "8.9.0",
39
+ "eslint-config-silverwind": "48.1.0",
40
+ "jest": "27.5.1",
41
+ "updates": "13.0.0",
42
+ "versions": "9.2.1"
47
43
  }
48
44
  }