cidr-tools 10.0.1 → 10.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.
package/README.md CHANGED
@@ -78,6 +78,7 @@ Returns a `parsed` Object which is used internally by this module. It can be use
78
78
  - `ip` String: The IP address inside the CIDR, including any `%scopeid` if present.
79
79
  - `version` Number: IP protocol version. Either `4` or `6`.
80
80
  - `prefix` String: The network prefix, e.g. `64`.
81
+ - `prefixPresent` Boolean: Whether the passed string has a network prefix.
81
82
  - `start` BigInt: Start number of the network.
82
83
  - `end` BigInt: End number of the network.
83
84
 
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ type ParsedCidr = {
10
10
  ip: string;
11
11
  version: ValidIpVersion;
12
12
  prefix: string;
13
+ prefixPresent: boolean;
13
14
  start: bigint;
14
15
  end: bigint;
15
16
  };
@@ -21,8 +22,8 @@ export declare function normalizeCidr(cidr: Network, opts?: NormalizeOpts): Netw
21
22
  export declare function normalizeCidr(cidr: Network[], opts?: NormalizeOpts): Network[];
22
23
  export declare function parseCidr(str: Network): ParsedCidr;
23
24
  export declare function mergeCidr(nets: Networks): Network[];
24
- export declare function excludeCidr(base: Networks, excl: Networks): string[];
25
- export declare function expandCidr(nets: Networks): string[];
25
+ export declare function excludeCidr(base: Networks, excl: Networks): Network[];
26
+ export declare function expandCidr(nets: Networks): Network[];
26
27
  export declare function overlapCidr(a: Networks, b: Networks): boolean;
27
28
  export declare function containsCidr(a: Networks, b: Networks): boolean;
28
29
  declare const _default: {
@@ -35,3 +36,4 @@ declare const _default: {
35
36
  parseCidr: typeof parseCidr;
36
37
  };
37
38
  export default _default;
39
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAIA,KAAK,WAAW,GAAG,MAAM,CAAC;AAC1B,KAAK,WAAW,GAAG,MAAM,CAAC;AAC1B,KAAK,QAAQ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CAAC,EAAE,QAAQ,GAAG,OAAO,CAwB9D;;;;;;;;;;AAED,wBAQE"}
package/dist/index.js CHANGED
@@ -17,8 +17,8 @@ function compare(a, b) {
17
17
  }
18
18
  }
19
19
  function doNormalize(cidr, { compress = true, hexify = false } = {}) {
20
- const { start, end, prefix, version } = parseCidr(cidr);
21
- if (start !== end) {
20
+ const { start, end, prefix, version, prefixPresent } = parseCidr(cidr);
21
+ if (start !== end || prefixPresent) {
22
22
  const ip = normalizeIp(stringifyIp({ number: start, version }), { compress, hexify });
23
23
  return `${ip}/${prefix}`;
24
24
  } else {
@@ -56,6 +56,7 @@ function parseCidr(str) {
56
56
  parsed.ip = stringifyIp({ number, version, ipv4mapped, scopeid });
57
57
  parsed.cidr = `${parsed.ip}/${prefix}`;
58
58
  parsed.prefix = prefix;
59
+ parsed.prefixPresent = Boolean(cidrVer);
59
60
  const numBits = bits[version];
60
61
  const ipBits = number.toString(2).padStart(numBits, "0");
61
62
  const prefixLen = Number(numBits - Number(prefix));
@@ -65,17 +66,13 @@ function parseCidr(str) {
65
66
  return parsed;
66
67
  }
67
68
  function doNetsOverlap(a, b) {
68
- if (a.start > b.end)
69
- return false;
70
- if (b.start > a.end)
71
- return false;
69
+ if (a.start > b.end) return false;
70
+ if (b.start > a.end) return false;
72
71
  return true;
73
72
  }
74
73
  function netContains(a, b) {
75
- if (b.start < a.start)
76
- return false;
77
- if (b.end > a.end)
78
- return false;
74
+ if (b.start < a.start) return false;
75
+ if (b.end > a.end) return false;
79
76
  return true;
80
77
  }
81
78
  function excludeNets(a, b, v) {
@@ -110,8 +107,7 @@ function excludeNets(a, b, v) {
110
107
  return mergeCidr(remaining);
111
108
  }
112
109
  function biggestPowerOfTwo(num) {
113
- if (num === 0n)
114
- return 0n;
110
+ if (num === 0n) return 0n;
115
111
  return 2n ** BigInt(String(num.toString(2).length - 1));
116
112
  }
117
113
  function subparts(part) {
@@ -155,8 +151,7 @@ function subparts(part) {
155
151
  return parts;
156
152
  }
157
153
  function diff(a, b) {
158
- a += 1n;
159
- return a - b;
154
+ return a + 1n - b;
160
155
  }
161
156
  function formatPart(part, version) {
162
157
  const ip = normalizeCidr(stringifyIp({ number: BigInt(part.start.toString()), version }));
@@ -167,20 +162,10 @@ function formatPart(part, version) {
167
162
  function mapNets(nets) {
168
163
  const maps = { 4: {}, 6: {} };
169
164
  for (const { start, end, version } of nets) {
170
- if (!maps[version][String(start)])
171
- maps[version][String(start)] = {};
172
- if (!maps[version][String(end)])
173
- maps[version][String(end)] = {};
174
- if (maps[version][String(start)].start) {
175
- maps[version][String(start)].start += 1;
176
- } else {
177
- maps[version][String(start)].start = 1;
178
- }
179
- if (maps[version][String(end)].end) {
180
- maps[version][String(end)].end += 1;
181
- } else {
182
- maps[version][String(end)].end = 1;
183
- }
165
+ if (!maps[version][String(start)]) maps[version][String(start)] = { start: 0, end: 0 };
166
+ if (!maps[version][String(end)]) maps[version][String(end)] = { start: 0, end: 0 };
167
+ maps[version][String(start)].start += 1;
168
+ maps[version][String(end)].end += 1;
184
169
  }
185
170
  return maps;
186
171
  }
@@ -192,16 +177,11 @@ function doMerge(maps) {
192
177
  const merged = [];
193
178
  for (const [index, number] of numbers.entries()) {
194
179
  const marker = maps[String(number)];
195
- if (start === null && marker.start) {
196
- start = BigInt(number);
197
- }
198
- if (marker.end) {
199
- end = BigInt(number);
200
- }
201
- if (marker.start)
202
- depth += marker.start;
203
- if (marker.end)
204
- depth -= marker.end;
180
+ if (start === null && marker.start) start = BigInt(number);
181
+ if (marker.end) end = BigInt(number);
182
+ if (start === null) continue;
183
+ if (marker.start) depth += marker.start;
184
+ if (marker.end) depth -= marker.end;
205
185
  const next = numbers[index + 1];
206
186
  if (marker.end && depth === 0 && next && BigInt(next) - BigInt(number) > 1) {
207
187
  for (const sub of subparts({ start, end })) {
@@ -233,13 +213,11 @@ function excludeCidr(base, excl) {
233
213
  const excls = { 4: [], 6: [] };
234
214
  for (const basenet of basenets) {
235
215
  const version = cidrVersion(basenet);
236
- if (version)
237
- bases[version].push(basenet);
216
+ if (version) bases[version].push(basenet);
238
217
  }
239
218
  for (const exclnet of exclnets) {
240
219
  const version = cidrVersion(exclnet);
241
- if (version)
242
- excls[version].push(exclnet);
220
+ if (version) excls[version].push(exclnet);
243
221
  }
244
222
  for (const v of [4, 6]) {
245
223
  for (const exclcidr of excls[v]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "10.0.1",
3
+ "version": "10.1.1",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR",
6
6
  "repository": "silverwind/cidr-tools",
@@ -17,19 +17,18 @@
17
17
  "node": ">=18"
18
18
  },
19
19
  "dependencies": {
20
- "ip-bigint": "^8.1.0"
20
+ "ip-bigint": "^8.2.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/node": "20.12.12",
24
23
  "eslint": "8.57.0",
25
- "eslint-config-silverwind": "85.1.5",
26
- "eslint-config-silverwind-typescript": "3.2.8",
27
- "typescript-config-silverwind": "4.4.0",
28
- "updates": "16.1.1",
24
+ "eslint-config-silverwind": "89.0.1",
25
+ "eslint-config-silverwind-typescript": "6.0.4",
26
+ "typescript-config-silverwind": "5.1.3",
27
+ "updates": "16.2.1",
29
28
  "versions": "12.1.2",
30
- "vite": "5.2.12",
31
- "vite-config-silverwind": "2.1.4",
32
- "vitest": "1.6.0",
33
- "vitest-config-silverwind": "9.0.6"
29
+ "vite": "5.3.4",
30
+ "vite-config-silverwind": "3.0.0",
31
+ "vitest": "2.0.4",
32
+ "vitest-config-silverwind": "9.1.0"
34
33
  }
35
34
  }