cidr-tools 6.4.1 → 6.4.2

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 (3) hide show
  1. package/index.d.ts +0 -2
  2. package/index.js +18 -20
  3. package/package.json +9 -8
package/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import exp from "constants";
2
-
3
1
  type IPv4Address = string;
4
2
  type IPv4CIDR = string;
5
3
  type IPv6Address = string;
package/index.js CHANGED
@@ -4,8 +4,8 @@ import naturalCompare from "string-natural-compare";
4
4
  import {parseIp, stringifyIp, normalizeIp} from "ip-bigint";
5
5
 
6
6
  const bits = {
7
- "v4": 32,
8
- "v6": 128,
7
+ 4: 32,
8
+ 6: 128,
9
9
  };
10
10
 
11
11
  const uniq = arr => Array.from(new Set(arr));
@@ -52,7 +52,7 @@ export function parse(str) {
52
52
  } else {
53
53
  const version = isIP(str);
54
54
  if (version) {
55
- parsed.cidr = `${str}/${bits[`v${version}`]}`;
55
+ parsed.cidr = `${str}/${bits[version]}`;
56
56
  parsed.version = version;
57
57
  parsed.single = true;
58
58
  } else {
@@ -63,7 +63,7 @@ export function parse(str) {
63
63
  const [ip, prefix] = parsed.cidr.split("/");
64
64
  parsed.prefix = prefix;
65
65
  const {number, version} = parseIp(ip);
66
- const numBits = bits[`v${version}`];
66
+ const numBits = bits[version];
67
67
  const ipBits = number.toString(2).padStart(numBits, "0");
68
68
  const prefixLen = Number(numBits - prefix);
69
69
  const startBits = ipBits.substring(0, numBits - prefixLen);
@@ -229,7 +229,7 @@ function diff(a, b) {
229
229
  function formatPart(part, v) {
230
230
  const ip = normalize(stringifyIp({
231
231
  number: BigInt(part.start.toString()),
232
- version: Number(v.substring(1)),
232
+ version: v,
233
233
  }));
234
234
  const zeroes = diff(part.end, part.start).toString(2);
235
235
  const prefix = bits[v] - (zeroes.match(/0/g) || []).length;
@@ -237,10 +237,8 @@ function formatPart(part, v) {
237
237
  }
238
238
 
239
239
  function mapNets(nets) {
240
- const maps = {v4: {}, v6: {}};
241
- for (const {start, end, version} of nets) {
242
- const v = `v${version}`;
243
-
240
+ const maps = {4: {}, 6: {}};
241
+ for (const {start, end, version: v} of nets) {
244
242
  if (!maps[v][start]) maps[v][start] = {};
245
243
  if (!maps[v][end]) maps[v][end] = {};
246
244
 
@@ -263,11 +261,11 @@ export function merge(nets) {
263
261
  nets = uniq((Array.isArray(nets) ? nets : [nets]).map(parse));
264
262
  const maps = mapNets(nets);
265
263
 
266
- const merged = {v4: [], v6: []};
267
- const start = {v4: null, v6: null};
268
- const end = {v4: null, v6: null};
264
+ const merged = {4: [], 6: []};
265
+ const start = {4: null, 6: null};
266
+ const end = {4: null, 6: null};
269
267
 
270
- for (const v of ["v4", "v6"]) {
268
+ for (const v of [4, 6]) {
271
269
  const numbers = Object.keys(maps[v]).sort(naturalCompare);
272
270
  let depth = 0;
273
271
 
@@ -298,7 +296,7 @@ export function merge(nets) {
298
296
  }
299
297
  }
300
298
 
301
- return [...merged.v4.sort(naturalCompare), ...merged.v6.sort(naturalCompare)];
299
+ return [...merged[4].sort(naturalCompare), ...merged[6].sort(naturalCompare)];
302
300
  }
303
301
 
304
302
  export function exclude(basenets, exclnets) {
@@ -308,18 +306,18 @@ export function exclude(basenets, exclnets) {
308
306
  basenets = merge(basenets);
309
307
  exclnets = merge(exclnets);
310
308
 
311
- const bases = {v4: [], v6: []};
312
- const excls = {v4: [], v6: []};
309
+ const bases = {4: [], 6: []};
310
+ const excls = {4: [], 6: []};
313
311
 
314
312
  for (const basenet of basenets) {
315
- bases[`v${isCidr(basenet)}`].push(basenet);
313
+ bases[isCidr(basenet)].push(basenet);
316
314
  }
317
315
 
318
316
  for (const exclnet of exclnets) {
319
- excls[`v${isCidr(exclnet)}`].push(exclnet);
317
+ excls[isCidr(exclnet)].push(exclnet);
320
318
  }
321
319
 
322
- for (const v of ["v4", "v6"]) {
320
+ for (const v of [4, 6]) {
323
321
  for (const exclcidr of excls[v]) {
324
322
  for (const [index, basecidr] of bases[v].entries()) {
325
323
  const base = parse(basecidr);
@@ -333,7 +331,7 @@ export function exclude(basenets, exclnets) {
333
331
  }
334
332
  }
335
333
 
336
- return bases.v4.concat(bases.v6);
334
+ return bases[4].concat(bases[6]);
337
335
  }
338
336
 
339
337
  export function expand(nets) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "6.4.1",
3
+ "version": "6.4.2",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR",
6
6
  "repository": "silverwind/cidr-tools",
@@ -19,16 +19,17 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "cidr-regex": "4.0.3",
22
- "ip-bigint": "7.2.1",
22
+ "ip-bigint": "7.3.0",
23
23
  "ip-regex": "5.0.0",
24
24
  "string-natural-compare": "3.0.1"
25
25
  },
26
26
  "devDependencies": {
27
- "eslint": "8.42.0",
28
- "eslint-config-silverwind": "73.0.2",
29
- "tsd": "0.28.1",
30
- "updates": "14.1.1",
31
- "versions": "11.0.1",
32
- "vitest": "0.32.0"
27
+ "eslint": "8.49.0",
28
+ "eslint-config-silverwind": "74.1.4",
29
+ "tsd": "0.29.0",
30
+ "updates": "15.0.2",
31
+ "versions": "11.1.0",
32
+ "vitest": "0.34.4",
33
+ "vitest-config-silverwind": "3.0.0"
33
34
  }
34
35
  }