cidr-tools 6.2.1 → 6.4.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.
Files changed (4) hide show
  1. package/README.md +17 -1
  2. package/index.d.ts +32 -12
  3. package/index.js +14 -2
  4. package/package.json +4 -3
package/README.md CHANGED
@@ -11,7 +11,7 @@ $ npm i cidr-tools
11
11
  ## Example
12
12
 
13
13
  ```js
14
- import {merge, exclude, expand, overlap, contains, normalize} from 'cidr-tools';
14
+ import {merge, exclude, expand, overlap, contains, normalize, parse} from 'cidr-tools';
15
15
 
16
16
  merge(['1.0.0.0/24', '1.0.1.0/24']); //=> ['1.0.0.0/23']
17
17
  exclude(['::1/127'], ['::1/128']) //=> ['::/128']
@@ -19,6 +19,7 @@ expand(['2001:db8::/126']) //=> ['2001:db8::', '2001:db8::1', '2001:db8::2', '20
19
19
  overlap('1.0.0.0/24', '1.0.0.128/25') //=> true
20
20
  contains(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true
21
21
  normalize('::ffff/64') //=> '::/64'
22
+ parse('::/64'); // => {cidr: '::/64', version: 6, prefix: '64', start: 0n, end: 18446744073709551615n}
22
23
  ```
23
24
 
24
25
  ## API
@@ -70,4 +71,19 @@ Returns a string or array (depending on input) with a normalized representation.
70
71
  - `compress`: Whether to compress the IP. For IPv6, this means the "best representation" all-lowercase shortest possible form. Default: `true`.
71
72
  - `hexify`: Whether to convert IPv4-Mapped IPv6 addresses to hex. Default: `false`.
72
73
 
74
+ ### parse(network)
75
+
76
+ - `network` *String*: A CIDR or IP address.
77
+
78
+ Returns a `parsed` Object. Specifically, this can be used to test whether the passed CIDR or IP is IPv4 or IPv6.
79
+
80
+ `parsed`: `Object`
81
+ - `cidr` String: The CIDR of the network.
82
+ - `version` Number: Either `4` or `6`.
83
+ - `prefix` String: The network prefix, e.g. `/64`.
84
+ - `start` BigInt: Start of the network.
85
+ - `end` BigInt: Start of the network.
86
+ - `single` Boolean: `true` when is a single IP.
87
+
88
+
73
89
  © [silverwind](https://github.com/silverwind), distributed under BSD licence.
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import exp from "constants";
2
+
1
3
  type IPv4Address = string;
2
4
  type IPv4CIDR = string;
3
5
  type IPv6Address = string;
@@ -6,17 +8,35 @@ type IPv6CIDR = string;
6
8
  type Network = IPv4Address | IPv4CIDR | IPv6Address | IPv6CIDR;
7
9
  type Networks = Network | Network[];
8
10
 
9
- interface CIDRTools {
10
- merge(networks: Networks): Network[];
11
- exclude(baseNetworks: Networks, excludeNetworks: Networks): Network[];
12
- expand(networks: Networks): Network[];
13
- overlap(networksA: Networks, networksB: Networks): boolean;
14
- normalize(cidr: Networks): Networks;
15
- contains(networksA: Networks, networksB: Networks): boolean;
16
- }
11
+ type Parsed = {
12
+ cidr: string;
13
+ version: number;
14
+ prefix: string;
15
+ start: bigint;
16
+ end: bigint;
17
+ single: boolean;
18
+ };
19
+
20
+ type NormalizeOpts = {
21
+ compress?: boolean;
22
+ hexify?: boolean;
23
+ };
17
24
 
18
- declare const cidrTools : CIDRTools;
25
+ export function merge(networks: Networks): Network[];
26
+ export function exclude(baseNetworks: Networks, excludeNetworks: Networks): Network[];
27
+ export function expand(networks: Networks): Network[];
28
+ export function overlap(networksA: Networks, networksB: Networks): boolean;
29
+ export function normalize(cidr: Networks, opts?: NormalizeOpts): Networks;
30
+ export function contains(networksA: Networks, networksB: Networks): boolean;
31
+ export function parse(network: Network): Parsed;
19
32
 
20
- declare module "cidr-tools" {
21
- export = cidrTools;
22
- }
33
+ declare const _default: {
34
+ merge: typeof merge;
35
+ exclude: typeof exclude;
36
+ expand: typeof expand;
37
+ overlap: typeof overlap;
38
+ normalize: typeof normalize;
39
+ contains: typeof contains;
40
+ parse: typeof parse;
41
+ };
42
+ export default _default;
package/index.js CHANGED
@@ -10,7 +10,7 @@ const bits = {
10
10
 
11
11
  const uniq = arr => Array.from(new Set(arr));
12
12
 
13
- function isIP(ip) {
13
+ export function isIP(ip) {
14
14
  if (ipRegex.v4({exact: true}).test(ip)) return 4;
15
15
  if (ipRegex.v6({exact: true}).test(ip)) return 6;
16
16
  return 0;
@@ -41,9 +41,11 @@ export function normalize(cidr, {compress = true, hexify = false} = {}) {
41
41
  }
42
42
  }
43
43
 
44
- function parse(str) {
44
+ export function parse(str) {
45
45
  const cidrVersion = isCidr(str);
46
46
  const parsed = Object.create(null);
47
+ parsed.single = false;
48
+
47
49
  if (cidrVersion) {
48
50
  parsed.cidr = str;
49
51
  parsed.version = cidrVersion;
@@ -395,3 +397,13 @@ export function contains(a, b) {
395
397
 
396
398
  return numFound === numExpected;
397
399
  }
400
+
401
+ export default {
402
+ merge,
403
+ exclude,
404
+ expand,
405
+ overlap,
406
+ contains,
407
+ normalize,
408
+ parse,
409
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "6.2.1",
3
+ "version": "6.4.0",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR",
6
6
  "repository": "silverwind/cidr-tools",
@@ -14,8 +14,8 @@
14
14
  "node": ">=16"
15
15
  },
16
16
  "files": [
17
- "index.js",
18
- "index.d.ts"
17
+ "./index.js",
18
+ "./index.d.ts"
19
19
  ],
20
20
  "dependencies": {
21
21
  "cidr-regex": "4.0.3",
@@ -26,6 +26,7 @@
26
26
  "devDependencies": {
27
27
  "eslint": "8.42.0",
28
28
  "eslint-config-silverwind": "73.0.2",
29
+ "tsd": "0.28.1",
29
30
  "updates": "14.1.1",
30
31
  "versions": "11.0.1",
31
32
  "vitest": "0.32.0"