ip-utilties 1.2.0 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -12,6 +12,7 @@
12
12
  },
13
13
  "description": "ip v4/v6 utility functions",
14
14
  "keywords": [
15
+ "cidr",
15
16
  "ip",
16
17
  "ipv4",
17
18
  "ipv6",
package/src/ip.mjs CHANGED
@@ -51,9 +51,7 @@ function _encode(family, address) {
51
51
  const res = new family.factory(family.segments);
52
52
 
53
53
  let i = 0;
54
- for (const segment of family
55
- .normalize(address)
56
- .split(family.separator)) {
54
+ for (const segment of family.normalize(address).split(family.separator)) {
57
55
  res[i++] = parseInt(segment, family.base);
58
56
  }
59
57
 
@@ -148,7 +146,7 @@ export function isIPv6(address) {
148
146
  * @return {string|undefined}
149
147
  */
150
148
  export function familyIP(address) {
151
- return _family(address)?.name
149
+ return _family(address)?.name;
152
150
  }
153
151
 
154
152
  function _family(address) {
@@ -162,8 +160,7 @@ function _is(family, address) {
162
160
 
163
161
  case "object":
164
162
  return (
165
- address instanceof family.factory &&
166
- address.length === family.segments
163
+ address instanceof family.factory && address.length === family.segments
167
164
  );
168
165
  }
169
166
 
@@ -203,8 +200,7 @@ export function prefixIP(address, length) {
203
200
 
204
201
  function _prefix(family, address, length) {
205
202
  return (
206
- _asBigInt(family, address) &
207
- (-1n << BigInt(family.bitLength - length))
203
+ _asBigInt(family, address) & (-1n << BigInt(family.bitLength - length))
208
204
  );
209
205
  }
210
206
 
@@ -218,18 +214,23 @@ export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
218
214
  ];
219
215
  }
220
216
 
217
+ export function matchPrefixIP(prefix, length, address) {
218
+ const family = _family(address);
219
+ return _prefix(family, address, length) === _prefix(family, prefix, length);
220
+ }
221
+
221
222
  export function normalizeCIDR(address) {
222
223
  let [prefix, prefixLength] = address.split(/\//);
223
224
  let longPrefix;
224
225
 
225
- if (!prefixLength && isLinkLocal(address)) {
226
+ prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
227
+
228
+ if (isLinkLocal(address)) {
226
229
  prefix = "fe80::";
227
230
  longPrefix = prefix;
228
231
  prefixLength = 64;
229
232
  } else {
230
- prefixLength = parseInt(prefixLength);
231
-
232
- const family = isIPv6(prefix) ? ipv6 : ipv4;
233
+ const family = /*_family(prefix); */ isIPv6(prefix) ? ipv6 : ipv4;
233
234
  let n;
234
235
 
235
236
  if (prefixLength) {
@@ -239,8 +240,6 @@ export function normalizeCIDR(address) {
239
240
 
240
241
  if (isLocalhost(n)) {
241
242
  prefixLength = family === ipv6 ? 128 : 8;
242
- } else {
243
- return {};
244
243
  }
245
244
  }
246
245
  prefix = _decode(family, n, prefixLength);
package/types/ip.d.mts CHANGED
@@ -15,12 +15,8 @@ export function familyIP(address: string | Uint8Array | Uint16Array): string | u
15
15
  export function asBigInt(address: any): any;
16
16
  export function prefixIP(address: any, length: any): string;
17
17
  export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
18
+ export function matchPrefixIP(prefix: any, length: any, address: any): boolean;
18
19
  export function normalizeCIDR(address: any): {
19
- longPrefix?: undefined;
20
- prefix?: undefined;
21
- prefixLength?: undefined;
22
- cidr?: undefined;
23
- } | {
24
20
  longPrefix: any;
25
21
  prefix: any;
26
22
  prefixLength: any;